Question about "return"


Hello, I am wondering if a lua function requires a return? The functions I am referring to are what you would call a void function in C# or Java so a return would just be there to prevent any weird errors from happening.

For example

local function ChangeNumber()
    workspace.NumberValue.Value = 5

I am wondering if I need to do this:

local function ChangeNumber()
    workspace.NumberValue.Value = 5

I currently have not been putting returns in “void” functions but I have been getting some weird bugs in my games. I am wondering if this is the cause.

Lua automically puts a return at the end for every function so they end after completion, so you don’t need to put a return there if you’re not returning anything

1 Like

If you want the function to return a certain value, then you need to use the return with the return value right after it. Otherwise, all functions will automatically terminate after they are completed.

In lua there is no need to manually add a return in a function unless you need to return something for example:

function test()
return 5

local value = test()
Print(value) --this prints 5

also yes functions without return automatically end when its finished, and automatically “return” nothing

No, returns are not required. If you leave out the return statement, the function returns nil.

you can do something like:

function aFunction()
for i,v in next, game.Players:GetPlayers() do

retun v.Name


return is like break (stopping functions) and returning an value, nil, object,end and more

Ok, thanks!

This post was too short…