(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!