How do you completely kill a thread?

Is there a way to kill an entire thread like an error without clogging output? I want to have a function that checks whether a player is occupied then kills whatever thread of functions was currently being ran, so I don’t need a bunch of “if occupied” checks within a bunch of different functions.

2 Likes

Are you thinking about something like [break or return]?

Like return but for everything. Because if you just do a return you still need to compare it within the original script that checked whether you were occupied, while I want it to effectively stop everything right there on the spot ( like error() )

return
print'hi' --> doesnt print

Example:

local foo = function()
  while true do
     if os.time() > 1 then
        return true
     end
  end
end

local result = foo()
if foo() then
   return
end

print'hi' --> doesnt print

Not what I need. I need to cancel out the entire thread so if I have a function running a function running a function, everything has to “cancel” on the spot, again, like an error.

If I have, say, 1 function that checks whether or not you’re occupied, that then runs another function that has to check if you’re occupied…
I’d end up overcomplicating everything and at that point I might as well just run a loop through every function in my module to check whether you’re occupied or not before continuing the normal function.

Maybe coroutine.yield?

	print("1") --prints
	print("2") --prints
	coroutine.yield()
	print("3") --doesnt print

2 Likes

Yield just pauses the thread. You’d end up with a memory leak.

Edit: (besides it needs to be a coroutine in the first place.)

1 Like

Hmm. Maybe you’re thinking of destroying the script completely?

I essentially just need the effects of an error, where the entire thread of the currently running (function) dies, without throwing an error in the output.
It’s an OOP module so destroying it is completely out of the picture.

Maybe just use return? I am pretty sure that kills a function/thread.

just the function/thread that is running

1 Like

Yeah. If you want something otherwise, create a bindablevent inside the module or the script, and fire it, and make every thread that you want to kill receive it, and use return on that thread when you receive the event.

maybe just create a bool variable, then make every function/thread check if the variable is true then everything will return. However this may create many wait loops

1 Like

With return:

local occupied = false
function step1()
	if occupied then return end
	--do stuff
	if not step2() then return end
	--do more stuff
end
function step2()
	if occupied then return end
	--do stuff
	if not step3 then return end
	--do more stuff
end
function step3()
	if occupied then return end
	--do stuff
end

With a thread being killed

function checkIfOccupied()
	if occupied  then
		--kill thread
	end
end
function step1()
	checkIfOccupied()
	step2()
end
function step2()
	checkIfOccupied()
	step3()
end
function step3()
	checkIfOccupied()
end

Like open up a baseplate and try using “error” instead of return. The entire thread of the original function ends. That means if “checkIfOccupied” kills the thread at step3, it kills the entire thread, meaning step3 gets halted, step2 gets halted and step1 gets halted. This can be done with an error put outputs garbage.

just make the checkoccupied return false if occupied, and then the other functions look if it’s false, then they just return nil and don’t run the other part.

That is precisely what I’m trying to avoid by having it kill the thread… I’m not looking for “how to make it work” I’m looking for how to avoid having five million if checks all embedded one in another and getting way out of hand if it can be simplified to 1 line.
If I need to check if it’s occupied, that’s fine, it’s just gonna get messy real fast when it’s inside an oop class where one function can lead to many other functions that all can possibly not work if occupied.

Well I am sorry, but that’s what you have to do.

It isn’t. I’ve already explained “error()” does this. I just want an alternative that doesn’t spam output.

As you can see return and break are the only ways, but return only works in functions:

Go in a game right now and run this code.

function Debug(str, str2)
	print(str)
	Debug2()
	print(str2)
end
function Debug2()
	if true then error() end
end
Debug(1, 2)