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

how do i stop this touched event from running too many times?

Asked by
Jumbuu 110
8 years ago
blade.Touched:connect(function(hit)


    local damageCal = math.random(damage,maxDamage)
    if equipped and clicked and character and humanoid and humanoid.Health > 0 and hit and not hit:IsDescendantOf(character) then
        local targetHumanoid = hit.Parent:FindFirstChild'Humanoid'
        if targetHumanoid and targetHumanoid.Health > 0 then
            targetHumanoid:TakeDamage(damageCal)
            print(damageCal)

        end
    end

end)


the script above triggers 3 hit colisions each time and i want to know how to limit it to one

3 answers

Log in to vote
2
Answered by 8 years ago

It seems to me the above answers forgot the wait before resetting the debounce

Debounces are used a lot. They are used a ton for touched events like in your situation. I'm not going to go through the whole process of explaining what a debounce is, but I would recommend looking at the wiki about it.

To fix your code, do what the others have done, but with a wait before resetting the debounce.

local debounce = false

blade.Touched:connect(function(hit)
    if not debounce then
    debounce = true
    local damageCal = math.random(damage,maxDamage)
    if equipped and clicked and character and humanoid and humanoid.Health > 0 and hit and not hit:IsDescendantOf(character) then
        local targetHumanoid = hit.Parent:FindFirstChild'Humanoid'
        if targetHumanoid and targetHumanoid.Health > 0 then
            targetHumanoid:TakeDamage(damageCal)
            print(damageCal)

             end

         end
      end
      wait(3)-- make the cooldown as long as you want.
      debounce = false
end)
The wait acts as a "cooldown", to where the code will not be ran again until the wait has finished.

I hope I helped!

Good Luck!

Ad
Log in to vote
3
Answered by
rexbit 707 Moderation Voter
8 years ago

The answer would be Debounce, what a debounce is basically a pause act.

local debounce = false

blade.Touched:connect(function(hit)
    if not debounce then
    debounce = true
    local damageCal = math.random(damage,maxDamage)
    if equipped and clicked and character and humanoid and humanoid.Health > 0 and hit and not hit:IsDescendantOf(character) then
        local targetHumanoid = hit.Parent:FindFirstChild'Humanoid'
        if targetHumanoid and targetHumanoid.Health > 0 then
            targetHumanoid:TakeDamage(damageCal)
            print(damageCal)

             end

         end
      end
      debounce = false
end)

It detects if debounce is considered false first, then reads the code, but after the if statement, there is a debounce = true this switches the debounce variable to true making the code read once till debounce is set to false at the bottom of the code.

0
yes this works but it still triggers 3 hit collisions Jumbuu 110 — 8y
0
You forgot the wait :P User#11440 120 — 8y
Log in to vote
-3
Answered by 8 years ago

Debounce: What this does is stop the event from firing once it has started so if they touch it once, debounce will be set to true and now if they touch it again it Will not fire the function since debounce is now true, it will start that way until the function is finished Need More Help, Here is the wiki

local debounce = false

blade.Touched:connect(function(hit)

 if debounce == false then 
debounce = true -- Now they wont be able to do it again
    local damageCal = math.random(damage,maxDamage)
    if equipped and clicked and character and humanoid and humanoid.Health > 0 and hit and not hit:IsDescendantOf(character) then
        local targetHumanoid = hit.Parent:FindFirstChild'Humanoid'
        if targetHumanoid and targetHumanoid.Health > 0 then
            targetHumanoid:TakeDamage(damageCal)
           print(damageCal)


       end
        end
    end
debounce = false
end)

Answer this question