Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I don't really get what returning does and I'm very confused with it?

I'm way more advanced than this, but returning was always my weak spot. What exactly does it do? When are you supposed to use it? When I try to watch a video on it, they just use printing, I get it with printing but not other stuff. If possible please write a script using return that can use printing but just don't make it too simple.

1 answer

✓ Accepted answer

It's actually a super simple concept.

When you return something in a function, that function just holds the value that it returned (Kinda like a variable would).

For example, if you did this:

local function Add() --Create the function
	return 2+2
end


local solution = Add() --Now we call it, it runs, and returns 2+2. That solution is 4. 


print(solution) 


-->>Output: 4

There are tons of other things you can do with returning. The basic idea though is that when you return something, the function holds that value. No code will run after something is returned.

For example:

local function func()
	print("Hey")
	return 
	print("Heyyy")
end

--Expected output: Hey

The second "Heyyy" wouldnt print, because the function ends at return. Im also pretty sure Lua would throw an error if you tried to run code after a return, but don't quote me on that.

I'm sure there's someone that can explain it a LOT better than I can internally, because I'm not that good at knowing what goes on under the hood of it all.

In conclusion, when you return something within a function, the function basically takes that values reference in its place, sorta like a variable. You can also use returns as a similar solution to using break with loops and such, since no code runs after you return something.

6 comments

0

If you need any clarifications, let me know. I've always been bad at explaining stuff :p

1

@Psudar well why and when would you use it? I get the idea of returning, its just when to use it

0

Its actually really useful. Nearly every default function in roblox returns something. For example, if you use :GetChildren(), that returns a table of all the children of the instance you used it on. Its really good when you want a function to calculate something (in the mathematical sense, as well as loosely meaning).

0

If I had to recommend anything, just go in and mess around with returning. This was also something I struggled with understanding, as well as arguments/parameters. eventually it just clicked, and I got a lot better at programming because of it.

1

An example of when to use a return thing for me would be to use a RemoteFunction to return data to see if a player had data or not. If they didn't have data, I would return "No data", and if they had data, I would return "Data". This determined if they could actually load a game or not

1

Also a good use for it is to take local variables out a function

This discussion is preserved as it was. New answers, comments, and votes are closed.