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:

1local part = --reminder to do this
2 
3part.Touched:connect(function(hit)
4    if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
5        hit.Parent.Humanoid:TakeDamage(1)
6    end
7end)

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

01local go = true
02 
03part.Touched:Connect(function(hit)
04    if go then
05        go = false
06        --We set go to false, so that if this event is fired before we set it to true after a second
07        --It won't get through the if statement
08 
09        print("Code here")
10 
11        wait(1)
12        go = true
13    end
14end
Ad
Log in to vote
0
Answered by
stepatron 103
6 years ago
Edited 6 years ago

Put a wait() Something like,

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

Sorry if I messed up, I'm on mobile

Answer this question