What is return and how do I use it?

I have seen lots of scripts use return and i’m unsure of its use. If you could explain it in simple words, it would be greatly appreciated!

1 Like

Use search: What is “return” and what’s its use in roblox scripting? - #5 by sjr04

1 Like

It returns a value and breaks the function, if it’s just return on its own, then it just breaks the function and returns nothing

1 Like

Thanks a lot for the link! 30 chars.

Thanks for the simple description! Life saver.

Example:

function x(f: string): string
   return f
end

print(x("Example"));

Above with the function, I’m just type checking, meaning in this case it should only take a string in and return a string

1 Like

Thank you so much!!! This helped a lot! :grinning:

“In computer programming , a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after the instruction which called the subroutine, known as its return address.”

local function DoMath(x,y)
    return 5+5;
end;

local FivePlusFive = DoMath(5,5);
print(FivePlusFive) --<Outputs 10

You can also use it to call functions within a function and pass arguments

local OnExecute = function(x)
    return function()
        print(x.." Ready :D");
    end;
end;

--//
local function SetReady(x,z)
    OnExecute(x); OnExecute(z);
end;

--//Let's Imagine 'SetupState' is a function that executes 'SetReady' After A Certain Criteria Or Period Of Time
local CurrentX, CurrentZ = "hello", "world"
SetUpState(a_argument, anotherone, SetReady(CurrentX,CurrentZ), true);

--// 'SetupState' Inside A Module Script
function module.SetupState(arg, arg, Function, arg)
    wait(5); Function();
end;

3 Likes

Thank you! That helped a ton! :pray:

1 Like