I’m really frustrated on returns. I have watched lots of videos about return and still don’t get it. I’m hoping someone can explain or reword what return means. Any help is appreciated!!
From my experience, there are 2 ways return
is used normally
You can use it to set a variable via returning something from a function, example
local function AddNums(x,y)
return x + y
end
local result = AddNums(10,5)
print(result) -- Prints 15 because the result of the function gets stored in the variable
print(AddNums(10,5)) -- This will also print 15 because again, it's returning a result anyways
You can also use to cancel an event mid execution
local part = script.Parent
part.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
if not hum then return end --No humanoid? cancel early
print("Humanoid found") --Wont print if there was no humanoid
end)
I have faced a lot of problems in Returning too when I first started scripting in roblox, Its actually not that hard but u need to practice the more u practice the more u understand it better, Briefly Returns let you return bool,int,string etc… ,so you can use it later in variable I’m not too good at explain stuff but I hope this can help Roblox Beginners Scripting Tutorial 7 - Returning - YouTube this is where I have learned returning from
Think of like return as if if you were to treat the function as a variable what would it return? What would that variables value be? Let’s get a bit obtuse so it will be clear but
local a = "b"
A way to represent this in a function is
local function a()
return "b"
end
Now when we do a()
its like doing the same as a
Return only works on functions (and modules, ignore that)
When calling a function, you can set it to a variable. Example:
local function getResult(n, e)
return n+e
end
local result = getResult(2, 5)
-- result is now 7!
They will also stop the function completely.
Pretty easy to understand I guess. They can be used in functions to get results, or anything really.
Of course you can do more than set to a variable, but also, calling a function inside an argument or something will just you whatever the function returned.
Return let’s you “return” anything, any data type. Tables, userdata, anything.
Some stuff like datastores can have problems in which they only support tables, strings, numbers, etc…
But returning it self doesn’t have that limitation. You can get anything from an return.
Return is the same as “give this back to whoever demanded me lol”
So basically return is like you return any value from the function so you can use it later in your script?
Hello, how are you doing?
Yeah, that’s basically it. Let’s say you have a function that multiplies or divides two numbers, returning would give the proper result.
local function Calculate(x, y, DorM)
--DorM
--Divide or Multiply
--If the value is true, it divides. If it's false it multiplies.
if DorM then
return x / y
else
return x * y
end
end
local Number = Calculate(2, 10, false)
print(Number)
--Prints the number 20
local Number2 = Calculate(10, 2, true)
print(Number2)
--Prints the number 5
Sorry if this isn’t the best way of explaining it, but return
sort of turns the function into a variable.
local function foo()
return 5
end
local var = foo()
print(var) -- 5
The function foo
now has a value, 5
, so when I set a variable to the result of me calling foo
, that variable will now equal 5.
local function foo()
end
local var = foo()
print(var) -- nil
A function that doesn’t return anything is called a void function - it doesn’t have a value. When you set a variable to the result of you calling a void function, the variable will equal nil.
I also had a problem with this when I started
Okay, return is basiclly what it is heard, it returns value/values.
function returntest()
return 'we got here!'
end
As you see, if you do: print(returntest)
it will print we got here!
Thats not only what it can do, but it could block the code:
local enabled = true
if enabled == false then
return
end
print(enabled)
As you see, if enabled is false, then the lines of code below will not run.
For short; you can use return
in functions to return a value, second, it will stop any line of code below it.
Buuuuut, if your code is:
print('yay')
return
print('yay2')
Than yay2 will never be ran, because no code under ‘return’ will run. You will get a Syntax error. But, you could still use it in functions. e.x:
function returnname(value)
if value == true then
return 'true!!!!!!!!'
end
end
Hoped this help, or you can visit the Developer Page on ‘return’ (sorry if I explained bad)
This is dumbing it down to the lowest I can go.
local function math(num)
if 5 + num == 10 then
return true
else
return false
end
end
if math(5) == true then print("i just did a math") end
Apologies if my indentation is off, I literally just made this in this post. Hope you understand!