Question about "return"

Question

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
end

I am wondering if I need to do this:

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

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
end

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

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

https://www.lua.org/pil/4.4.html

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
end
end

print(aFunction()

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

Ok, thanks!

This post was too short…