Which is better to use?

There’s 3 different ways to define a function in luau (as far as I know), but is there a way that’s best to use? (if so what is the difference between them)

Usually I define my functions like this:

function sayHello(player)
	local msg = string.format("Hello, %s", player.Name)
	print(msg)
end

game:GetService("Players").PlayerAdded:Connect(sayHello)

However there are other ways:

local function hi()
	print("Hi")
end

local bye = function()
	print("Bye")
end
2 Likes

The latter block is better, since it declares a local variable. The third example though is different from the second.

local function hi() end
-- is syntax sugar for
local hi; hi = function() end

local function hi() syntax allows for recursive function calls, local bye = function() won’t.

6 Likes