-
What do you want to achieve? i wanna know how to use return
-
What is the issue? i dont know how to use return
-
What solutions have you tried so far? nothing
Return just returns a value:
local function returnTwo()
return 2
end
local thisIsTwo = returnTwo()
print(thisIsTwo) --> prints "2"
what does it exactly mean to ‘return’ a value?
It sort of “gives” back a thing. For example, if you want to give an apple when somebody asks, it’s like returning an apple.
yea i see.
but is it important?
can i code anything without using it?
while not required, it is very useful as you can define a function to put in values and then get a processed value or you can run code and tell the requesting script what the result was. return can also be used to fetch a value from server to client with remote function. while you can code things without it there are many use cases for return which makes things easier or more modularized.
Sure, you can. It’s just easier to use “return” in some cases. Take this for example.
local Carrot = game.Workspace:FindFirstChild("Carrot")
if not Carrot then return end -- If the carrot doesn't exist, the line of code below will not run.
Carrot.Transparency = 0
Without returning:
local Carrot = game.Workspace:FindFirstChild("Carrot")
if not game.Workspace:FindFirstChild("Carrot") then
task.wait() -- Better to have a return here rather than task.wait() or just nothing at all. You can save such a lot of space from using return.
else
Carrot.Transparency = 0
end
This is a very basic example of what returning can do. I am still learning about returning myself, but it isn’t important to use all of the time. You can pretty much code anything you want to without returning being involved. Returning is just easier to use sometimes. Returning can prevent errors sometimes, especially if you aren’t sure an instance exists yet, such as my code at the very top of my comment for an example. Returning is very useful in some cases, but it’s not that big of a deal.
if you call a function, like say:
function getparts()
end
and you want to get the parts and use it as a variable, then you can use return;
function getparts()
local parts = game:GetDescendants()
local stuff = {}
for i,v in ipairs(parts) do
if v:IsA("BasePart") then
table.insert(stuff,v)
end
end
return stuff
end
so then, you can just say
local parts = getparts()
return is very useful for organizing your scripts and for when you want to use a function more than once, for example in UI functionality
(i didn’t mean to reply, missclick sorry)
yes. where ever you want to get data more than once, use a function with return
you can also use it for some more stuff: example of my gun framework
function module.new(toolname:string) -- guys this is module based im so cool right
local self = {}
self.equipped = false
self.arms = {}
self.gun = {}
self.toolname = toolname
return setmetatable(self, fpsMT)
end
its an example of where return can be useful. you likely wont be making complex systems like these so it doesnt matter. you can look up what these things are if you really want to understand it.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.