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
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)
I hope I helped!
Good Luck!
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.
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)