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

How do i make a delay to taking damage?

I was making a part that deals damage but since roblox runs at lots of ticks per second, the damage is too frequent for me to use like i wish to use it. I need help finding a way to delay the frequency of the damage i take to an amount of seconds that i choose.

Here's my damage script:

local part = --reminder to do this

part.Touched:connect(function(hit)
    if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
        hit.Parent.Humanoid:TakeDamage(1)
    end
end)

2 answers

✓ Accepted answer

I assume you want to use what we in the biz call debounce

local go = true

part.Touched:Connect(function(hit)
	if go then
		go = false
		--We set go to false, so that if this event is fired before we set it to true after a second
		--It won't get through the if statement

		print("Code here")

		wait(1)
		go = true
	end
end

Put a wait() Something like,

part.Touched:connect(function(hit)
    if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
        wait(5) --time it will wait in seconds
        hit.Parent.Humanoid:TakeDamage(1)
    end
end

Sorry if I messed up, I'm on mobile

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