Is having only 1 PlayerAdded in the whole game more efficient?

Greetings! While I was re-coding a bit I came to the idea of just having one PlayerAdded in the whole game inside of a Module Script which then triggers a function in every module I have added into my table. The script looks like this:

local ListeningHandler = {}

local AnimHandler = require(script.Parent:WaitForChild("AnimationsHandler"))

local SendPlayerAddedStatement = {
	AnimHandler.PlayerAdded
}

local SendCharacterAddedStatement = {
	AnimHandler.CharacterAdded
}

local SendPlayerRemovedStatement = {
	AnimHandler.PlayerRemoved
}

game.Players.PlayerAdded:Connect(function(plr)
	for _,module in SendPlayerAddedStatement do
		task.spawn(function()
			module(plr)
		end)
	end
	
	plr.CharacterAdded:Connect(function(char)
		for _,module in SendCharacterAddedStatement do
			task.spawn(function()
				module(char)
			end)
		end
	end)
end)

for _,plr in game.Players:GetChildren() do
	for _,module in SendPlayerAddedStatement do
		task.spawn(function()
			module(plr)
		end)
	end
	
	plr.CharacterAdded:Connect(function(char)
		for _,module in SendCharacterAddedStatement do
			task.spawn(function()
				module(plr)
			end)
		end
	end)
end

------------

game.Players.PlayerRemoving:Connect(function(plr)
	for _,module in SendPlayerRemovedStatement do
		task.spawn(function()
			module(plr)
		end)
	end
end)

Overall I was asking myself if this is now more efficient than having many PlayerAdded events in different scripts. Every answer is appreciated. :slight_smile:

2 Likes

Connections are extremely cheap in memory that the difference in performance is insignificant and negligible. It is more of a question if you would like to control which modules will execute first.

3 Likes

Ah I see, alright. Thank you for your response! :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.