Is While true loop suppose to completely end after using a return end statement

When using a while true statement with a return end statement, after the return end statement runs, the while true statement just stops working completely. Is it suppose to do that?

Example:

while true do
	task.wait(1)
	print("test 1")
	local findItem = workspace:FindFirstChild("Spawn")
	if findItem then
		findItem:Destroy()
		return
	end
end

In theory it should print “test 1” every second and delete Spawn block. But I only see it print “test 1” once with the spawn block being destroyed. Might be bug or intentional I’m not very good at scripting.

1 Like

yes. return will end the process (you can return some arguments if it’s inside a function.). additional text: break will end the current loop.

If your code is in a function, then yes. This is because a function only expects one return value. (When you don’t return, you’re still returning nil (in other words, nothing) at the end of the function)

EDIT: The script itself is technically a “function”, it is a process too. ‘return’ will stop the code.

while true loop is by itself, but I’m pretty sure p49p0 answered my question.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.