Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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!

2 comments

0

maybe u should make the wait period longer since its only 1 second

0

connect is deprecated, use Connect

1 answer

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.