Hello.
I don't understand when or how to use return. I see them used in lots of scripts, but it seems like they are not necessary. Could someone elaborate on how and when to use return?
Thanks,
Blue
Return.
Return is used for many things. It is used to make a function act as a variable/ have a value, prematurely end a function, or both. Take this for example:
local TextLabel = script.Parent -- Localscript under a textlabel function text() return "This text was returned by the function called 'text'!" end TextLabel.Text = text() -- Above line is the same as: TextLabel.Text = "This text was returned by the function called 'text'!"
Return can make functions act as objects, variables, boolean values, numbers, strings, enumerations, you name it.
If you wanted a part to be Anchored vía code, you can do this:
function Bool() return true end Part.Anchored = Bool() -- part is anchored
Return can return bool values from maths:
function returnBoolFromMaths(num1, num2, num3) return num1 + num2 == num3 end Part.Anchored = returnBoolFromMaths(1, 2, 3) -- part is anchored Part.Anchored = returnBoolFromMaths(1, 5, 98) -- not anchored
One thing to note is that when you use return, code cannot be written after it.
function stuff(thing) return thing print"Thing returned!" -- Will error. end
You can also perform basic maths with return:
function number() return 500 end print(1000-number()) -- > prints 500 since 1000-500==500
If you call a function which returns a bool value, it won’t error. This is what I mean:
--Using a bool value alone will error. Take this for example: local x = true function trueFunc() return true end x -- this alone will error true -- Same as above trueFunc() -- although equivalent to both above, this will not error.
Return can also be used to prematurely end a function like this:
function myFunc(service, player, id) if game:GetService(service):PlayerHasPass(player, id) then return game:GetService(service):PlayerHasPass(player, id) end return "Didn’t get there!" -- if it didn’t get to the other code, end this function. end myFunc( "GamePassService", -- too lazy to make this a variable lol game:GetService('Players').LocalPlayer, --this is a localscript that’s why we using localplr 000000000000 --random id )
You also need to use return for Remote Functions. Thanks to magicguy78942 for the idea:
-- code from ROBLOX Wiki: -- Server local ReplicatedStorage = game:GetService("ReplicatedStorage") local createPartRequest = Instance.new("RemoteFunction") createPartRequest.Parent = ReplicatedStorage createPartRequest.Name = "CreatePartRequest" local function onCreatePartRequested(player) print(player.Name, "wants to create a new part") local newPart = Instance.new("Part") newPart.Parent = game.Workspace return newPart -- returns the part. in the local script, :InvokeServer() acts as the part. end createPartRequest.OnServerInvoke = onCreatePartRequested
ROBLOX Wiki LocalScript:
-- LocalScript local ReplicatedStorage = game:GetService("ReplicatedStorage") local createPartRequest = ReplicatedStorage:WaitForChild("CreatePartRequest") local newPart = createPartRequest:InvokeServer() -- This acts as the part created in the OnServerInvoke callback because of the 'return newPart' print("The server created this part for me:", newPart)
"How and when should I use return?"
You should use return when you need to:
Return a value; End a function; Use remote functions.
There are more " when to use return’s" that I don’t know about.