Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Return Vrs Break?

Should I use Break or return to exit this function? which one is more data efficient and faster?

local regening = true

function regenHealth()
	if regening then
	return --break or return
	end
	regening = true
	print("Hp")
	wait(1)
	regening = false
end

Humanoid.HealthChanged:connect(regenHealth)

2 answers

✓ Accepted answer

This is just me, but I would recommend using a return instead of break. Breaks are usually meant to end loops (used in while-do's and repeat-until's).

Since your code is already formatted to meet what I answered, I would just leave it as it is.

Using a break in the body of a function isn't valid. You'll get an error

no loop to break near 'end'


break stops the containing loop which is either a repeat ... until, a for ... end, or while .... end.

return stops the containing function and optionally sends out values.

In particular, that means

-- "done" will NOT print
function blah()
	for i = 1, 10 do
		if i == 5 then
			return
		end
	end
	print("done")
end

-- "done" WILL print
function blah()
	for i = 1, 10 do
		if i == 5 then
			break
		end
	end
	print("done")
end

This discussion is preserved as it was. New answers, comments, and votes are closed.