I'm EXTREMELY confused about return

  1. What do you want to achieve? I want to know how to use return as I’m extremely confused about it.

  2. What is the issue? I don’t get how return works and why it is important.

  3. What solutions have you tried so far? I’ve tried watching some tutorials about it, but I still don’t get it. How to use it and why is it important?

5 Likes

Return just returns a value:

local function returnTwo()
    return 2
end

local thisIsTwo = returnTwo()
print(thisIsTwo) --> prints "2"
1 Like

Can you please clarify what do you mean by " returns a value "?

My main language isn’t English.

2 Likes

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.

3 Likes

I get it.

But is it really that important?

Like can you code any system without using it?

2 Likes

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.

2 Likes

Is it necessarily important for a person that has been coding for almost 6 months?

It’s not even relevant to my game development and I’ve never used it in my projects.

1 Like

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.

3 Likes

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)

1 Like

Wow.

I still don’t smoothly understand return actually, so my last question is, is it useful for me as a beginner scripter?

1 Like

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
1 Like

Idk how to use metatable.

Idk how to use self

Idk how to use modulescripts

Idk how to use self

Idk why are tables important and why do you use {} with no data inside it.

can you please recommend me a source to learn all these

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.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.