How to use spawn() without a function

Hello roblox!
I want to use spawn() without a function so, i could use return from spawn() to parent function
Here is what i want to do:

function foo()
	spawn(
		wait(1)
		return("will prevent function foo() running")
	)
	wait(2)
    --needs to be not ran
	print("i ran")
end
foo()

This would be a great feature to stop a function

1 Like
task.spawn(function()
	--your code here
end)

Unfortunately this won’t work given the nature of how threading in Roblox is handled.

An alternative solution is to set a flag (boolean variable) to true if you want your function to stop executing.

function foo()
wait(2)
if stopExecutionFlag then return end --//set elsewhere
print'yay'
end
1 Like

That’s bad, but thanks for telling me then.

You should also know that spawn is going to be deprecated soon, and you should switch to task.spawn() or coroutines for "parallel"ising code.