Should I use Connect(function() end) or Connect(onFunctionName)?

I’ve found two ways people connect events to functions. One makes an empty function so code can be slotted in immediately:

-- script to toggle visbility of a Gui Frame from a serverside event
game.ReplicatedStorage.ShowGui.OnClientEvent:Connect(function()
	script.Parent.Frame.Visible = not script.Parent.Frame.Visible -- toggle visibility
end)

But then I look at older scripts and older tutorials and find more of calling an individual function. This I’m more familiar with coming from C# and GDscript where you have these callback-type functions:

-- script to toggle visbility of a Gui Frame from a serverside event
function onTouched()
	script.Parent.Frame.Visible = not script.Parent.Frame.Visible
end

game.ReplicatedStorage.ShowGui.OnClientEvent:Connect(onTouched)

Is there a general preference for people using one or the other to do connections?

Just for context, this event is coming from a script in ServerScriptService that’s connecting a part to a ReplicatedStorage event:

game.Workspace.Part.Touched:connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		game.ReplicatedStorage.ShowGui:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
	end
end)

Specifically for developers working in groups, do you have rules for which type you use? Or is it just a non-issue to choose one over the other that you all understand what’s going on in both situations?

Many thanks.

2 Likes

Both usages are fine. The only time you would want to use one over another is when you have to connect to the same function to different RBXScriptSignals which in that case you just declare the function separately.
Example:

function onTouched(Part)
    print(Part)
end

for Index, Value in ipairs(game.Workspace:GetDescendants()) do
    if Value:IsA("BasePart") == true then
        Value.Touched:Connect(onTouched)
    end
end
4 Likes

You could use either since it’s just personal preference whether to make a function outside and connect it to the event or connect a new function immediately.

I typically make the function when I connect the event since it’s simpler for me to understand

as @BenMactavsin, the only time making a function and connecting it to an event is when you have to use it more than once, otherwise, it’s just preference

1 Like

Okay cool, that’s good to know ^ ^ I never realized you could also connect multiple parts with a for loop, so a function would make it way easier for sure. But it’s also good to know I could just do it without declaring a function first if I don’t really want to :stuck_out_tongue: I just generally prefer code that’s self-commenting so it’s less work to read it. Thanks!

The way you should do it is like this:

When you have to connect only time, use the (function()

When you need to connect the same function on multiple times / events, make it a variable and then connect the variable instead.

2 Likes

Only reason why I dont use

function onTouched()
	script.Parent.Frame.Visible = not script.Parent.Frame.Visible
end

game.ReplicatedStorage.ShowGui.OnClientEvent:Connect(onTouched)

is because I’m not good at naming things :stuck_out_tongue:

2 Likes

Using second is better for performance and speed.

1 Like