Basically, this just cancels the entire function ahead of time before the lines ahead of it can run
Say I have this:
local Variable = 20
local function CheckVariable()
if Variable == 20 then
print("This equals 20! CANCEL IT BEFORE THE FBI SPOTS IT")
return
end
print("This variable is not equal to 20, you're safe!")
end
CheckVariable()
In our if statement check, if a certain conditional check is true, then we can return (Or end) our function early since it’s our Variable is equal to 20 & will print that FBI statement
If it isn’t however, we can assume that the variable is pretty much safe since our variable is equal to something else!
but it only takes up a single line and doesn’t force you to indent your code. It’s good for a quick check at the top of a function to see if data is missing or some condition that would cause the function to error. It’s cleaner to abort rather than indent everything and match up another end.
Oh so if then return end is like breaking the function?
I actully am curious and see if you there can be anything printed after the function has been called
If you just wanna stop a certain function from preventing its other lines of code from running inside the function, you can just cancel the function early by using return
A long time ago, I also didn’t understand when it was needed and it took a month to finally learn why I needed it.
This is when you need it.
local function AddNumbers(firstNumber,secondNumber)
return firstNumber + secondNumber
end
local onePlusThree = AddNumbers(1,3)
print(onePlusThree)
--return returns something to the item that calls it
local function countTable(tbl)
return #tbl
end
print(countTable({1,2,3,4,5}))
allowed = false
if not allowed then
return
else
print("access granted")
end
--it can also end something