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
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
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.
1 comment
return goes a step back into the previous line...