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

Whats the difference between leaving something blank and returning something blank?

on one code we got

if game.Workspace.Name == 'Baseplate' then
	print('Yay')
	
else
	return 
end

and on the another

we got


 if game.Workspace.Name == 'Baseplate' then
	print('Yay')
	
else
	 
end

but they all do the same thing whats the point

1 comment

0

return goes a step back into the previous line...

1 answer

Return (without a value) can be used to exit a function early. This is especially useful to run checks before running a function without wrapping the whole thing in an if statement (which can get ugly!). For example:

function doadminstuff()
    if plr.name == admin then 
       print("Doing admin stuff!")
   end
end

can be rewritten as:

function doadminstuff()
    if not plr.name == admin then return end
    print("Doing admin stuff!")
end

If a player's name is not "admin", then the print line won't be run, because the function will exit early at the return statement. These sorts of statements keep things streamlined and organized, especially when you want to have multiple checks before you run a function.

Example:

function random()
    if not plr.name == "admin" then return end 
    if plr.color == "blue" then return end
    if time() > 1000 then return end

    print("I'm running after arbitrary checks!")
end

is a lot easier to understand than this abomination:

function random()
    if plr.name=="admin" and not plr.color == blue and time() < 1000 then
    	print("I'm running after arbitrary checks!")
    end
end

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