Is there a way to stop a function in say a script in the workspace, with a script in ServerScriptService. But stop the function at where it was even if it was doing a loop or something?
Im not exactly sure why you would do this and for most cases i would strongly recommend trying to find ways to avoid this. But code isnt an exact science and if REALLY wanna do it this way id probably use a signal like propertyChanged or attributechanged.
in the actual instance of script via properties menu you can add an Boolean attribute called “StopFunction” or something like that
then in the function you can say something like
local function myFunction()
local stopListener
stopListener = script:GetAttributeChangedSignal('StopFunction'):Connect(function()
local stopped = script:GetAttribute('StopFunction')
if not stopped then return end
script:SetAttribute('StopFunction', false)
stopListener:Disconnect()
return
end)
--Function code here.
end
then from an external script you referance the path and then set the stop function attribute to true
local scriptIWantToStop = game.ServerScriptService.scriptIWantToStop
scriptIWantToStop:SetAttribute('StopFunction', true)
Now that i have presented this solution. Dont do this💀 This is one of those solutions that are so stupid it almost wraps back around to being smart. Use module scripts, code it in a way where it can self govern and stop itself. Anything else is better. But the way above WILL work👏🏿
that is possible but like what baseplate said it’s not recommended as it may make your code messy and confusing so quickly
you can use globals like _G or shared to store stuff that can be accessed across many scripts
example
workspace script
local function func()
while true do
print("TEST")
task.wait()
end
end
_G.thread = task.spawn(func) -- task.spawn() returns a thread that can be used for task.cancel()
SSS script
task.wait(2) -- stop the function after 2 seconds
-- we have to check if the thread exist if we arnot sure if it exist
if _G.thread then
task.cancel(_G.thread)
end
This is a much better way unrecommend way to do it. I don’t know why i didn’t think of this🫡
As you said its good to avoid, I completely remade the system using coroutines instead
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.