Returning. When and how to use it

(Apologies for the previously deleted topic, as I accidentally created it before I was finished.)

Hello programmers! Have you been wondering what returning does? Many programmers do, so you’re not alone! Do one of these scenarios fit your situation?

  • Did you check the documentation page but ultimately found nothing?
  • Did you use it in your code but still couldn’t find out what it did?
  • Did you ask for help or search for videos about it, but the answers were vague, or didn’t help?
  • Some other scenario?

If you answered yes to any of the following questions, don’t lose hope, because this topic is for you! Let’s go over the basics of returning.

Breaking loops.

Take a look at the following code.

local RunService = game:GetService("RunService")

local Part = Instance.new("Part")
Part.Name = "Bob"
Part.Parent = workspace

while true do
	RunService.Heartbeat:Wait()

	if workspace:FindFirstChild("Bob") then
		print("The created part exists!")
	else
		return
	end
end

print("I ate a burger last night!")

For now, Bob exists, so this code will run. However, if I delete Bob, the printing will stop, and the code will no longer run, even if I created a new part and named it “Bob”. This is useful for when you want to stop code from running completely if a specific thing occurs, like if an NPC dies or if a player stops moving. Unlike break, the following line that contains print("I ate a burger last night!") will not run.

Note: (break is recommended in while loops rather than returning.)

Keep in mind that if you use the following code in an event, like this one below for example, the code will continue to run, which means that if I created a new part and named it “Bob”, then the script will keep running.

local RunService = game:GetService("RunService")

local Part = Instance.new("Part")
Part.Name = "Bob"
Part.Parent = workspace

RunService.Heartbeat:Connect(function(deltaTime)
	if workspace:FindFirstChild("Bob") then
		print("The created part exists!")
	else
		return
	end
end)

print("I ate a burger last night!") -- Because this isn't outside of a loop like the first code example, this will be the first thing that is delivered to the output.

Note: If you use a for loop, using return will still stop the script completely.

Returning values.

Ever wondered how to get a value out of a function? With returning, you can! Take a look at the following code.

function GeneratePart()
	local Part = Instance.new("Part")
	Part.Name = "Sally"
	Part.Parent = workspace
	
	return Part
end

local Sally = GeneratePart()
print(Sally.Name)

“Sally” will be delivered to the output! This function does not require parameters, but if you want, you can return parameters too!

function GeneratePart(ChosenName, ChosenParent)
	local Part = Instance.new("Part")
	Part.Name = ChosenName
	Part.Parent = ChosenParent
	
	return Part, ChosenName, ChosenParent
end

local Sally, ChosenName, ChosenParent = GeneratePart("Sally", workspace)
print(Sally.Name.." exists.", ChosenName, ChosenParent.Name)

All of these parameters will be delivered to the output. You can do so much more with this too! Now I know what some of you might be thinking. “I don’t like all of those variables in one line. How do I get all of them with one?” No worries. OH TABLESSSSSSS!!!!!!!!!!!!

function GeneratePart(ChosenName, ChosenParent)
	local Parameters = {}
	
	local Part = Instance.new("Part")
	Part.Name = ChosenName
	Part.Parent = ChosenParent
	
	Parameters.Sally = Part
	Parameters.Name = ChosenName
	Parameters.Parent = ChosenParent
	
	return Parameters
end

local Params = GeneratePart("Sally", workspace)
print(Params.Sally.Name.." exists.", Params.Name, Params.Parent)

End.

That’s all I have for today programmers! I hope this tutorial helped you and enlightened you on some of the things that returning can do. Thanks for reading! :grin:

5 Likes

This demonstration really helped, thank you.

2 Likes

This went so in depth, yet really simple. Thank you!

1 Like

Dont use return to break while loops. Use break instead.

2 Likes

Did you read the text? :star_struck:

He specifically said “while” not “for” loops. Also putting returns inside a for loop would just return the value being returned (that is if it’s within scope of a function), like:

local function get()
  for _, value in ipairs(someDict) do
    if value == 1 then
      return -1
    end
  end
  return 0
end

print(get({0, 3, 2, 1)) -- returns -1
print(get({1, 1, 3, 5}) -- returns 0
1 Like

Very ironic. I suggest you listen to your own advice.

3 Likes

yeah yeah made a mistake pass it towards :star_struck:

A simple google search can get you this page.
https://www.lua.org/pil/4.4.html

There are a ton of videos and other forum posts about return, break, and continue.

Do not use the keyword return in loops unless you want to exit the function or end the thread.

1 Like

@memeason Glad I could help!

@LaGoodDoge You’re welcome! :grinning:

@commitblue Good idea.

@lnconcinnity Oh, I was not aware of this. Thanks for that input.

@TheRealANDRO I don’t understand the point of this reply. Could you explain please?