For a long time, either when working on a combat system or literally anything else, I’ve wondered how to be able to stop a function that is meant to change a certain value after a time. I found the following post from someone that somewhat has my problem:
The first answer to the post is something I can put into practice, but when I actually tried I failed. Example of what I’m looking for:
local value = 1
function ChangeValue()
task.wait(2)
value = 2
end)
function ChangeValue2()
task.wait(4)
value = 1
end)
ChangeValue() -- Let's say I run my function meant to overwrite the variable 'value', but then if a condition is met, I change my mind and for example, I decide that I wanna make the wait between changing the value longer, how would I be able to stop the wait of the function and never actually set my variable to two?
If number == 1 then
ChangeValue2()
end
This code example isn’t optimal to explain my point, since I this isn’t a hard problem to come out with a solution for, but is when I want to make the player unable to walk if they’re stunned or it is when I wanna change an attribute after a certain time. I know I must use coroutines, but how would I apply that to what I want? I don’t want the code for it, I just want an explanation how to do it (Since I don’t fully understand coroutines even after researching about it for 3 days I fail to put into practice what I learned and what I understood from the post I addressed earlier)