I want to create a stun system, so when a player hits another player (with a weapon or projectile or something) it stuns them by setting their humanoid’s walkspeed to 0.
So:
-Player gets hit
-Stuns that player and sets a wait() timer
-Player gets unstunned
However, when I’ve done this. I’ve run into the problem of setting off this event too many times and it overlapping itself, creating some funky results. Is there anyway to restart a function if it’s already being run or something of the sort?
I might be over-complicating it, but thanks in advance. I couldn’t seem to find anything when I searched it up.
local debounce = false
part.Touched:Connect(function(part)
part.Touched:Connect(function(part)
if not debounce then
debounce = true
wait(3) -- cooldown
end
debounce = false
end)
I’m still new to scripting..
Could you explain what exactly the script is doing?
I can see it’s testing for the debounce, if it’s not true, it sets it to true and sets a timer.
And depending on whether or not the timer plays, the debounce is set to false either right away, or after 3 seconds? Is that right?
Also was the double function on purpose?
My trouble is that once my wait() timer is running, I want to stop it from running the code it will afterwards IF it runs the function again.
Yea I didn’t mean to duplicate the double function.
Since I set a wait time to 3, the first time the part gets touched, it will set debounce to true. Then, in the next three seconds, when the part gets touched again it will not get past the first if statement. This will only apply to any scripts you put in the code block of the if statement.
You could try searching it up on youtube. There might be a better explanation there.
The issue is that the wait() function still runs ALL of it’s code whether or not it’s set again, with or without debounce.
Making it so that the player gets stunned, waits three seconds… and then gets unstunned. Which, my bad for not really explaining things correctly.
But I want it so you can extend the stunlock, so, if you keep up a combo, you could stun them for longer than three seconds.
I guess the three seconds debounce thing still could pull it off, but there might be some bugs maybe with being able to get out of combos potentially every three seconds if not stunlocked perfectly again?
Which is kinda why I want to know if it’s possible to stop a function mid-run.
Which would still un-stun the character after three seconds… rather than extend his time being stunned. Think stunning in a street-fighter game, those kind of combos are my goal, this wouldn’t really fix it.
It just prevents it from being run multiple times, which, is one of the issues, yes. But I want it to be able to continually stun the character.
Thanks for your help, even though you did have two mess-ups in your code… which confused me at first. I might just create a new thread, or research some things to see if I can find a solution. Unless I’m not understanding what’s going on in that code.