Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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

2 comments

0

I don't think there will be any time difference, thanks for the answer!

0

Makes sense in my head but It probaly doesnt work that way anyways xD

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.