When I first started, I really struggled with returning too. But its really simple I promise.
First of all, returning only works with functions (and module scripts but ignore that for now, you can learn about it later) so here is an example of a function:
local function ThisIsAFunction()
end
To keep it simple, returning will do 2 things. Firstly, it will stop the function. example:
local function ThisIsAFunction()
print("This will print")
return -- stop function
print("This wont print")
end
Secondly, it returns something. For example, if you wanted to return the number 2, you would say:
local function ThisIsAFunction()
return 2
end
You can infact return multiple things (arguments):
local function ThisIsAFunction()
return 1,2,3,"A","B","C",true,false
end
so what exactly is Returning and how do you use it? its hard to explain so i will use an example.
you know how to create a part with a script?
local Part = Instance.new("Part")
believe it or not, this is a function. craazzyyyyyyyyyy! and it actually returns the part you created to that variable called “Part”. Part.Transparency Part.Parent
ok ok ok. so now *how exactly do i get this returned item from a function?
it is done through a variable (like above)! wooooooowwwww
example:
local function ThisIsAFunction()
return 2
end
local returnedthingy = ThisIsAFunction()
“OKOKOKWHAT?” you may ask. very suprisingly, saying this: ThisIsAFunction() anywhere in the code, will run the function, just so you know.
local function ThisIsAFunction()
return 2
end
local returnedthingy = ThisIsAFunction()
print(returnedrhingy)
anyways, what do you think this prints?
answer
2
how about this?:
local function ThisIsAFunction()
return "69"
end
local returnedthingy = ThisIsAFunction()
print(returnedrhingy)
answer
“69”
so you get the point.
B U T - T H E R E S - M O R E . . .
you can return many things.
example:
local function ThisIsAFunction()
return 1,2,3,4
end
local returnedthingy = ThisIsAFunction()
print(returnedrhingy)
what you think this prints?
hint: its not (1,2,3,4) suprisingly
answer
1
“BUT WAIT… WHAT?”
…yes, to get multiple returned items / arguments. you actually need multiple variable. like this:
local function ThisIsAFunction()
return 1,2,3,4
end
local returned1,returned2,returned3,returned4 = ThisIsAFunction()
print(returned1, returned2, returned3, returned4)
wat u think this prints? hint: its 1,3,2,4
answer
sike! I was joking
it prints 1,2,3,4
(don’t doubt yourself if u did)
thats it, there are more complex things u can do with returning (read other comments) but this is to help u understand.
if u dont understand, dont be discouraged, wana know how i understood? ;-; I - d i d n ’ t . . . it took me 2 years to learn it. you will get there 