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

How and when should I use return?

1 answer

✓ Accepted answer

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.

4 comments

0

Return is also important if you want to boost the performance while using RemoteFunctions!

0

Adding that in my answer! Will credit you.

0

Thank you all for explaining this.

0

Return is one of my favorite keywords throughout all languages tbh.

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