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

How do i make a delay to taking damage?

Asked by 6 years ago
Edited 6 years ago

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

Log in to vote
0
Answered by
theCJarmy7 1293 Moderation Voter
6 years ago
Edited 6 years ago

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
Ad
Log in to vote
0
Answered by
stepatron 103
6 years ago
Edited 6 years ago

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

Answer this question