So, I have been confused of what return is and what it is used for. So I have created this topic to ask. What is the purpose of returning and what can it do and when or why should I use it.
Return ends the execution of a function, and returns a value that is passed
function returnSum(A, B)
return A + B -- Will give the sum of A and B and execute the function
end
if returnSum(1, 3) < 10 then
return --Will stop the code scop from running
end
- Please go learn the basics of the programming language before attempting anything.
It may not sound nice however is the truth when I say this: You will look dumb if you ask these kinds of questions without bothering to learn the fundamentals of LUA/LUAU (LUAU is Roblox’s modified version of LUA) LUA Docs: Lua: documentation
- Look below for an example:
Its in the name. It returns whatever is after it. You can pretty much return whatever and how many things you want in whatever shape or way.
For example:
function example(a, b) -- takes 2 arguments (a and b) which is used and returned later.
print(a + b) -- print the result of a + b
print({a + b, a - b, a, b}) -- prints the same data that is returned by the function
return {a + b, a - b, a, b} -- returns as a json table
end
print(example(2, 3)) -- prints the result returned from the example function
Output:
01:03:09.280 5 - Server - Script:3
01:03:09.280 ▼ {
[1] = 5,
[2] = -1,
[3] = 2,
[4] = 3
} - Server - Script:4
01:03:09.281 ▼ {
[1] = 5,
[2] = -1,
[3] = 2,
[4] = 3
} - Server - Script:9
If you want to change it into something like an array you can do that by replacing the {} to [].
If you wanted to return any instances you can do that too just parse the instance to the return like:
local instance = game:GetService("Workspace"):GetChildren() -- get all instances In workspace
function example()
return instance -- returns whatever 'instance' is
end
print(example()) -- print the output of example
print(instance) -- compare the output of example and the result of instance
Im still learning the fundamentals btw…
I am just saying which is why I linked the official Lua docs website so you can actually learn something.
if returnSum(1, 3) < 10 then
return --Will stop the code scop from running
end
This will error. If you’re gonna give some examples please give something that is not going to error when they throw it into their studio and run it trusting it will work first try.
The function above it is fine though. (Saying for other newbies who are reading.)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.