How would I pause/stop a player's function or script?

gtg sleep i will check this tomorrow

assuming u want to disconnect or connect it based on a boolean u can do something like this

local state = false
local event = nil

game:GetService("RunService").Hearbeat:Connect(function()
  if not state and not event then
    event = part.Touched:Connect(function()

    end) 
  elseif state then
    pcall(function()
     event:Disconnect()
     event = nil
    end
  end
end)
1 Like

i tried to make a test:

local touchconnect

local newthread = function()
wait(1)
print("one")
wait(1)
touchconnect:Disconnect()
print("two")
wait(1)
print("three")
end

newthread()
touchconnect = (newthread)

I got an error and I was trying to test it, how would it work on a regular function? I wanna disable my whole entire function ( OnServerEvent ) so idk how to disable normal functions/threads. This test gave me an error (i am new to this disconnect thing) : ServerScriptService.Script:7: attempt to index function with ‘Disconnect’

When you start a function it doesn’t make a new thread. Luau is functional, so when you set touched connection to the function it sets the value of it to a function. It has to be a RBLXConnection to call disconnect on it.

If you want a function to stop mid way through you’d need checks after every yield (ex wait()). You can use Disconnect to stop a connection.

so for example i can do

local connection = event.OnServerEvent:Connect(function()

and

connection:Disconnect()

?

1 Like

help: https://devforum.roblox.com/t/re-how-would-i-pause-stop-a-players-function-or-script/970284/3?u=lilzy7

You could try this:

local func = coroutine.create(function() -- coroutines can create new threads which can be yielded whenever you want
-- code for the function
end)

local stopVal = Instance.new("BoolValue", player) -- bool values can hold true or false statements
stopVal.Name = "Stop"
stopVal.Value = false

stopVal:GetPropertyChangedSignal("Value"):Connect(function()

if stopVal.Value then
coroutine.yield(func) -- stop the function
func = nil
end

end)

coroutine.resume(func) -- call the function

More about coroutines:
Beginners Guide to Coroutines (roblox.com)

Coroutines - When and how to use them - Resources / Community Tutorials - DevForum | Roblox

1 Like

I searched about coroutines and people said you cant yield outside the coroutine???

Is this causing an issue in the script? If not then I wouldn’t really worry about it.

an example i did relating to your answer:

local func = coroutine.create(function()
wait(1)
print("one")
wait(1)
print("two")
wait(1)
print("three")
end)

coroutine.resume(func)
wait(1)
coroutine.yield(func)

This did not work, as it printed one two and three. I already searched and coroutines can’t be stopped from the outside only the inside.

What about ModuleScripts? They can come in handy for things like this.

i dont know what that is and how to use it.

Disconnecting doesn’t work, it only stops it from starting, not mid-way.

So your solution is putting a value and an if return end statement every wait, but that is very tedious and messy, is there any other way

make sure to reconnect the event

The disconnect makes it so the function doesnt work for new inputs, but the thread running inside it keeps going…

Hi, I learned more about module scripts and this post was a long time ago but how would I use a module script for this problem?

corutines, bindable event

reccomend checking if the player has a bindable event per say called “Cancel”

upon it being fired use corutine.close to close it
boom works just fine for me