Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Sword does not do any damage. I used touched event with function. Please help?

Asked by 6 years ago

So I have a function on my sword that makes it so when it touches someone, they are supposed to take damage, but nothing seems to happen. Also, the handle is the whole sword. Please help.

Here is my script:

local sword = script.Parent local handle = sword:FindFirstChild("Handle") local event = sword:FindFirstChild("RemoteEvent") local player = sword.Parent.Parent local humanoid = player:FindFirstChild("Humanoid")

handle.Touched:connect(function(hit) if player and humanoid and humanoid.Health > 0 then local hitHumanoid = hit.Parent:FindFirstChild("Humanoid") if hitHumanoid and hitHumanoid.Health > 0 then hitHumanoid:TakeDamage(50) end
end end)

0
please use code block lol LukeGabrieI 73 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

Please put your code in a code block next time. Anyways this should help:

local sword = script.Parent
local handle = sword:WaitForChild("Handle") -- Wait instead so it doesn't error if it hasn't loaded it
local event = sword:WaitForChild("RemoteEvent") -- Same applies here

handle.Touched:connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid and humanoid.Health > 0 then
        hitHumanoid:TakeDamage(50) end
    end
end)

Haven't tested this so it might not work. Cheers,

0
The hitHumanoid part should've been humanoid but otherwise it worked. Thanks! SweetNoodleSoup 14 — 6y
0
Your welcome! Crazycat4360 115 — 6y
Ad

Answer this question