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

Oh okay, do something like this:
local connection = part.Touched:Connect(onTouched)
wait(3)
connection:Disconnect()

You need to store the value returned by the connect function.
Written on mobile, sorry for the lack of formating

Edit:

Helpful Resources:

An event (in Roblox) is formally called a RBXScriptSignal:

When you connect an event it returns a connection (aka RBXScriptConnection):

You can then call :Disconnect on this connection to disconnect it (note this stops more threads from being created, doesn’t stop existing ones).

my function isnt in a while true do loop

He is using a function not a while loop. break() only works for loops

disconnect? what does that mean? you can disconnect functions?

You can disconnect event listeners, look into rbx event signals

1 Like

https://developer.roblox.com/en-us/recipes/How-to-disconnect-an-event-connection

so i would disconnect the function if a boolvalue changed to false or something? or a while true do loop checking it?

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.