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

Why is Ting/Debounce only working first time through script?

Asked by 4 years ago

So this is a basic take damage script. Because of how often players can hit stuff, even 1 damage is op without a ting/debounce. Well so i put in one, and the results are funky.. I upped the ting time for testing reasons. I got hit once, waited 20 seconds, and then I was able to get hit as many times as i wanted, as if ting didn't exist anymore after the first touch.. I even printed ting and it always came back as 1..

Anyone know why?

01local touch = script.Parent:FindFirstChild("Head")
02 
03 
04local ting = 0
05 
06function dmg(hit)
07    if ting == 0 then
08    ting = 1
09    if hit.Parent ~= nil then
10        local hum = hit.Parent:findFirstChild("Humanoid")
11        if hum ~= nil then
12            hum.Health = hum.Health - math.random(12,20)
13        end
14        end
15    end
16    wait(20) -- Here is my wait between activations
17    ting = 0
18end
19 
20touch.Touched:connect(dmg)

1 answer

Log in to vote
0
Answered by 4 years ago

Nevermind, I'm just tired I guess.

For anyone who wants a working damage script

01local touch = script.Parent:FindFirstChild("Head")
02 
03 
04local ting = 0
05 
06function dmg(hit)
07    if ting == 0 then
08     if hit.Parent ~= nil then
09        local hum = hit.Parent:findFirstChild("Humanoid")
10            if hum ~= nil then
11            ting = 1
12            hum.Health = hum.Health - math.random(12,20)
13            wait(1)
14            ting = 0
15            end
16     end
17    end
18end
19touch.Touched:connect(dmg)
Ad

Answer this question