Title pretty much explains it. What is the difference between “if something return” and “if not something then” And how can i use these effectively? And how are these two things any different?
Help much appreciated!
Title pretty much explains it. What is the difference between “if something return” and “if not something then” And how can i use these effectively? And how are these two things any different?
Help much appreciated!
These statements aren’t comparable as they do very different things. Basically return passes a value to the function call and exits the function when that happens. An if not something statement runs when a value is false. Are you trying to use return as an exit statement?
local function Announce(name)
if not name then
return
end
print("Hello, my name is "..name)
end
local function Announce(name)
if name then
print("Hello, my name is "..name)
end
end
The “if something return” you are talking about is referring to a guard clause.
When you have a lot of nested if functions that check if conditions are valid, things can get messy quickly. Instead, by calling return
when data is invalid, the function stops computing and you don’t have to worry about nesting all of your logic inside of multiple if statements.
The advantage is clear in a more messy example:
local function CheckData(d1, d2)
if not d1 or not d2 then
return
end
if not d1.SomeData or not d2.SomeData then
return
end
if d1.SomeData < d2.SomeData then
return
end
-- do something
end
local function CheckData(d1, d2)
if d1 and d2 then
if d1.SomeData and d2.SomeData then
if d1.SomeData >= d2.SomeData then
-- do something
end
end
end
end
Imagine you want to do a check:
local first = 10
local second = 20
local third = first + second
print(third) --Print 30
You can write it that way, but you better write it that way:
local first = 10
local second = 20
local function arithmetic(1, 2)
return 1 + 2
end
print(arithmetic(first, second)) --30
Return is like the Enter button right on your keyboard. If you type 10 + 20 on a calculator and then press the button to calculate the sum, this button is the return in Roblox “don’t worry, I’ve been using Roblox studio for a year now and still haven’t fully understood it. In time, you’ll understand”. You can use it to have strings (I’ve never used it like this before, but it’s always important to know everything I find):
local Name = "Ethanthegrand14"
local function GetName(name)
return "My Name is Ethanthegrand14"
end
print(Name) --Ethanthegrand14
print(GetName) --My Name is Ethanthegrand14
The lines after Return will not be executed (because of computer rules or something like that):
local function WhileLoop()
while wait(1) then
print("Script is running")
return --After this line the script will not be executed.
end
end
WhileLoop() --It will print only 1 Message and not infinity long messages
In this case you can use the return as break and the two will be about the same except that with return you can do one thing that the break cannot do:
if true then
print("Hi") --print Hi
return
print("Hi") --print nothing
end
Let’s try break:
if true then
print("Hi") --print nothing
break --ERROR
print("Hi") --print nothing
end
The break can only be used in loops, but return practically anywhere. I hope it helped you.
Edit: Just like Fabio said, you can make a loop with it that controls all elements, if something isn’t what you wanted it to be, the function will nil, but otherwise it will be something:
local function ForLoop()
for i = 1, 10, 1 do
if i == 1 then
return nil
end
if i == 2 then
return nil --We don't want it to happen with that.
end
if i == 3 then
return nil --We don't want it to happen with that.
end
if i == 4 then
return nil --We don't want it to happen with that.
end
if i == 5 then
return nil --We don't want it to happen with that.
end
if i == 6 then
return nil --We don't want it to happen with that.
end
if i == 7 then
return nil --We don't want it to happen with that.
end
if i == 8 then
return nil --We don't want it to happen with that.
end
if i == 9 then
return nil --We don't want it to happen with that.
end
if i == 10 then
return "10" --We want something to happen at 10.
end
end
end
print(ForLoop) --9X nil and 1X "10"
Two things about your last code sample:
Never actually do this in a production environment. Guard clauses should only be used a limited amount of times and rarely so in a loop. The only check you require is (i == 10) here.
You actually have to call ForLoop in the print statement, otherwise the memory address of the function will get printed instead of an actual result.
Sorry and thanks for this information!