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

Why does this simple damage script break when I add debounce?

Asked by 6 years ago

Hello, I've made a damage script in the past, and now I'm using it again. The damage script didn't have any debounce so I decided to add that, but then the script just broke entirely.


local debounce = false function OnTouch(damage) if not debounce then debounce = true damage.Parent:FindFirstChild("Humanoid").Health = damage.Parent:FindFirstChild("Humanoid").Health -50 wait(1) debounce = false end end script.Parent.Touched:connect(OnTouch)

(Damage script might look like a free model because I wasn't the best scripter back then)

Thanks for your time!

0
maybe u should make the wait period longer since its only 1 second 3dwardIsBack -10 — 6y
0
connect is deprecated, use Connect DinozCreates 1070 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

You need to check if the Humanoid really exists before attempting to change its health:

if damage.Parent:FindFirstChild("Humanoid") then
    damage.Parent:FindFirstChild("Humanoid").Health = damage.Parent:FindFirstChild("Humanoid").Health -50
end

If you don't, then everytime the Humanoid doesn't exist, the Health property won't exist too, so you get the error "attempt to index a nil value". As for debounce breaking your script, you only "turn off" debounce after you deal the damage, and since the error stops the function's execution, it ends up never finishing it

Besides that, your script looks fine to me (and actually works). Just make sure to use Tab when indenting. And as someone pointed out in the comments, connect is deprecated, use Connect.

Ad

Answer this question