Let’s say a function is running, and you want to stop it outside the function.
local function example()
for timer = 1, 10, 1 do
print(timer)
wait(1)
end
end
example()
In this case, the example() function is running. However, if I want to stop the function immediately, how would I do it? (Making a boolean that will return the function when true won’t stop it immediately, the wait(1) code will make the function wait for a second before checking if the boolean is true or false.)
I guess this could be an option which you can use:
local StopFunction = false
local function example()
for timer = 1, 10, 1 do
if StopFunction == false then
print(timer)
wait(1)
else
break -- if StopFunction is true, then the loop will break here
end
end
end
task.spawn(function()
example()
end)
task.wait(3)
StopFunction = true
If you wonder what task.spawn(function() is, it makes the example() and the lines under it run at the same time, so stopFunction = true can happen after 3 seconds (in my script because of the task.wait(3)), instead of waiting until the example() is finished with running. You could do some research about task.spawn(function() in the internet if you want.
You can change this script depending on how you want it to work if you want to.
Similarly to what @PVP_Player2373902 said, you have to call your function wrapped in task.spawn() and then you can cancel it whenever you want:
local newTask = task.spawn(example) --creating a new task with your example function
task.wait(10)
task.cancel(newTask) --cancelling the task after 10 seconds
So task.cancel stops the function? Guess I could try this:
local newTask = task.spawn(function()
for timer = 1, 10, 1 do
print(timer)
wait(1)
end
end)
task.delay(5)
exampleEvent:OnServerEvent:Connect(function() -- I want to stop the task through an event
task.cancel(newTask)
end
Just another question, how would I run the function again? I probably should have made this clearer earlier, but I want to rerun the function as the event is fired, not just stop it.
exampleEvent:OnServerEvent:Connect(function() -- I want to stop the task through an event
task.cancel(newTask)
newTask()
end
Is that a way of doing it…? Or wil it just resume the function
task.cancel() stops the task you created with task.spawn(). You are storing it in newTask just so you have a reference to the task so you can cancel it later, when cancelled you can’t run it again with just the reference, instead you have to create a new task:
local timerTask --a variable for storing the currently running task
local function example()
--your timer function
end
timerTask = task.spawn(example) --run your timer function wrapped in task.spawn()
exampleEvent:OnServerEvent:Connect(function()
task.cancel(timerTask) --cancel the task when event is fired
timerTask = task.spawn(example) --you can now assign a new task to the variable
end
Keep in mind that you should always cancel a task before assigning a new task to the same variable, as if you assign a new task before cancelling the old task, now you won’t have the reference to the old task and will not be able to stop it from running.
Another thing is the way you are using task.delay() is wrong, if you just want to yield the script for 5 seconds use task.wait(5) or wait(5) (but task.wait() is more precise). task.delay() is used to wait a certain amount of time before running a function like so:
task.delay(5,function()
--this function will run after 5 seconds
end)
task.delay() waits 5 seconds to run the function but it doesn’t yield the rest of the script, so essentially it does the same thing as doing:
task.spawn(function()
task.wait(5)
--your function
end)
Yes when using task.spawn() the function you provided will run immediately, if you want to wait a bit before running it you can use task.delay().
When function finishes task is finished too, if you have a reference to it stored somewhere you will still get the task, but won’t be able to do anything with it as its finished already, if you set the variable holding the reference to it to nil it will clean up the task.