When scripting, I start to run into the same problem where when I call functions from clicks and I need to wait before running the code, I only want the current function to run (for example, if the user spam clicks and multiple functions are called, I only want the most recent function to run, cancelling the previous ones).
Let's say when clicked, I wanted a text button's text size to turn 30, wait(5), then turn back to it's original size, 14.
textButton.MouseButton1Click:Connect(function()
textButton.TextSize = 30
wait(5)
textButton.TextSize = 14
end)
The problem is if the user only waited 4 seconds, then clicked it again, it wouldn't wait another 5 seconds, it would turn the size into 14 after 1 second because the previous function is still running. I want to make it so if a function is still running when a new one is called, the function that was running gets cancelled. This way if the user spam clicks, only the last click would count. It's kind of the opposite of a debounce if you think about it, I don't want only the first click to count, but the last one.
I've tried maaaaaaany different if-statements, and I've resolved this problem in the past using funky stuff, but I know there's definitely an easier way and I'm probably over-thinking it.

5 comments
Have a counter. Add one to it on each click.
Could you explain a little further?
I thinkin you also could use a debounce variable set true when your code run and wintg and the nset to false again
so you want the function to restart after the player clicks it again?
Yes, so if there were previous of the same functions called before then those would get cancelled. Basically a restart.