#Made a throwing knife, the knife hurts the player that threw it instead of the target

1 messages · Page 1 of 1 (latest)

craggy light
#

I made my own throwing knife. Right now, the knife hits the player that threw it and hurts them instead of hurting the target. When it does hit the target, the target doesn't take damage but I am assuming that is because of the debounce. Here is my code

local playerthrownid = script.Parent:GetAttribute("PlayerThrownId")
local debounce = false
script.Parent.Touched:Connect(function(hit)
    local Character = hit:FindFirstAncestorOfClass("Model")
    if Character and Character:FindFirstChild("Humanoid") then
        local Player = game.Players:GetPlayerFromCharacter(Character)
        local HitPlayerID = Player.UserId
        print(HitPlayerID)
        print(playerthrownid)
        if HitPlayerID ~= playerthrownid then
            if debounce == false then
                debounce = true
                local plr = hit.Parent
                local player = game.Players:GetPlayerFromCharacter(plr)
                local id = player.UserId
                game.ReplicatedStorage.ThrownKnifeHitSound:FireClient(player, id)        
                Character.Humanoid.Health -= 35
            end

        end
    end
end)
karmic night
#

Try

#

local playerThrownId = script.Parent:GetAttribute("PlayerThrownId")

local thrower = game.Players:GetPlayerByUserId(playerThrownId)
if thrower and thrower.Character then
for _, part in ipairs(thrower.Character:GetDescendants()) do
if part:IsA("BasePart") then
script.Parent.Touched:Wait()
part.CanTouch = false
task.delay(0.3, function()
if part then
part.CanTouch = true
end
end)
end
end
end

local hitOnce = false

script.Parent.Touched:Connect(function(hit)
local character = hit:FindFirstAncestorOfClass("Model")
if not character or not character:FindFirstChild("Humanoid") then return end

local hitPlayer = game.Players:GetPlayerFromCharacter(character)
if not hitPlayer then return end
if hitPlayer.UserId == playerThrownId then return end

if not hitOnce then
    hitOnce = true
    game.ReplicatedStorage.ThrownKnifeHitSound:FireClient(hitPlayer, hitPlayer.UserId)        
    character.Humanoid:TakeDamage(35)
    script.Parent:Destroy()
end

end)

Ts

quick frost
#

but i think i did it better

karmic night
#

Heh

quick frost
#

sec

#

local playerthrownid = script.Parent:GetAttribute("PlayerThrownId")
local hasHit = false

script.Parent.Touched:Connect(function(hit)
    if hasHit then return end

    local character = hit:FindFirstAncestorOfClass("Model")
    if not character then return end

    local humanoid = character:FindFirstChild("Humanoid")
    if not humanoid then return end

    local player = game.Players:GetPlayerFromCharacter(character)
    if not player then return end

    local hitPlayerId = player.UserId

    if hitPlayerId == playerthrownid then return end

    hasHit = true
    humanoid:TakeDamage(35)
    game.ReplicatedStorage.ThrownKnifeHitSound:FireClient(player, hitPlayerId)
end)
karmic night
#

Okay

quick frost
#

less lines

#

but yours is working too

karmic night
#

Does it matter

quick frost
#

so it doesnt matter ye

quick frost
#

😄

craggy light
#

Thank y'all. Figured out the problem was that my attribute wasnt being set properly.