Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Which option is best to keep an overview and will I have to use from now on?

Asked by 3 years ago

Hi, I would like to know which of the options below is the best for detecting when a player enters the game. Then I know immediately which one I will use from now on.

Option 1:

local Players = game:GetService("Players")

local function PlayerAdded(Player)

end

Players.PlayerAdded:Connect(PlayerAdded)

Option 2:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)

end)

2 answers

Log in to vote
1
Answered by 3 years ago
local Players = game:GetService("Players")

local function PlayerAdded(Player)

end

Players.PlayerAdded:Connect(PlayerAdded)

I really like the idea of this design and it would probably be cleaner. Performance-wise I think that option 2 would load quicker because it doesn't have to go through the function before the event if you know what I mean

0
I don't think there will be any time difference, thanks for the answer! Bankrovers 226 — 3y
0
Makes sense in my head but It probaly doesnt work that way anyways xD johnoscarbhv1 137 — 3y
Ad
Log in to vote
1
Answered by 3 years ago

They are very similar and in this case, they complete the same tasks. However option 1 allows many different events to fire that one function while option 2 only allows one single event to fire that specific function. For example: Option 1

local function SayHi()
    print("Hi")
end
game.Players.PlayerAdded:Connect(SayHi)
game.Players.PlayerRemoving:Connect(SayHi)

This would print Hi when the player leaves AND joins.

Answer this question