Anyone know a proper way to stop a function? For example, if I call a function and there is a timer and the player I don’t know maybe clicks a part and now I want the function to stop in its tracks. So therefore the function is technically done and nothing after where the function was will playout.
I tried using a disconnect line like people said but I’ve had zero luck.
EXAMPLE:
local function tp()
task.wait(10)
print(“Nope”)
end
Could I ask for the use case in which you’re trying to utilize this behaviour?
When talking about the Disconnect method, you’re thinking of RBXScriptConnections. An example of that would be
local Touched
Touched = workspace.BasePlate.Touched:Connect(function(otherPart)
-- Do something here
end)
local condition = true
if condition == true then
Touched:Disconnect()
end
@realmile has already given a good example on how you can achieve this. Another method is using conditions.
local condition = true
local function tp()
local timer = 0
while condition and (timer < 10) do
task.wait(1)
timer += 1
end
if not condition then
return
end
print('Nope')
end
condition = false
You can read about it all here task | Documentation - Roblox Creator Hub
But in general using task.wait() is more efficient than normal wait(), task.spawn() creates a function on a new thread which means it doesnt yield the code, instead it runs both the function and the rest of the code at the same time. task.cancel() stops a task you created, and task.delay() basically does same thing as task.spawn() but you also add a timer after how long it will run the function you provided.
Yes! So what I’m trying to do is when a player sits down(seat) it will fire a function which will teleport the player in per say 20 seconds. The catch is if the player gets up. I would want the entire function to be canceled so that the timer wouldn’t run down. It would just be finished without completing. If the function wouldn’t end, the player would be teleported as if they didn’t get up from that seat.
In that case, I would suggest using task.delay like @realmile mentioned, and an extra condition check before attempting to teleport the player.
local seated = true
local tp = task.delay(10, function()
if not seated then
print("User is not sitting here")
end
end)
I don’t know how you handle your sitting, but you may need to write your own logic on how to determine whether the player is still sitting down or not.
local CurrentOccupant = nil
local CurrentThread = nil
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if Seat.Occupant then
CurrentOccupant = Seat.Occupant
CurrentThread = task.delay(20, function()
CurrentOccupant:ChangeState(Enum.HumanoidStateType.Jumping)
tp(CurrentOccupant)
end)
else
if CurrentThread then
task.cancel(CurrentThread)
CurrentThread = nil
end
CurrentOccupant = nil
end
end)
To explain further on what @wf_sh has posted; task.delay() is a function itself which immediately run. You could write a wrapper to prevent the countdown from immediately starting
local seated = true
local function StartTeleporting()
task.delay(10, function()
if not seated then
print("User is not sitting here")
end
end)
end
StartTeleporting() -- The countdown starts here, and this allows you to start a new countdown any time you call this function.