Character List up-to-date

I am currently trying to achieve a function that would keep an updated list of alive players.
Are there any tips performance-wise that would help improve the code snip?

local function StartUp()
	local function CharLister(Child, State)
		if State == "Added" then
			table.insert(CharList, Child)
		elseif State == "Removed" then
			for i = 1, #CharList do
				if CharList[i].Name == Child.Name then
					table.remove (CharList, i)
				end
			end
		end
	end
	
	local function CharAdded(Char)
		CharLister(Char, "Added")
	end
	local function CharRemoved(Char)
		CharLister(Char, "Removed")
	end
	 
	local function PlrAdded(Plr)
		Plr.CharacterAdded:Connect(CharAdded)
		Plr.CharacterRemoving:Connect(CharRemoved)
	end

	Players.PlayerAdded:Connect(PlrAdded)
	for i, Plr in pairs(Players:GetChildren()) do
	    PlrAdded(Plr)
		if not Plr.Character then
		    Plr.CharacterAdded:wait()
		    CharAdded(Plr.Character)
		else
		    CharAdded(Plr.Character)
		end
	end
end
1 Like

By just glancing through the code, it seems just fine how it is. It appears everything is event-based, which is right in my book. There should be no problems with the snippet of code you provided, performance-wise. If anything, it is great the way you have it. Trying to improve it any further would fall under unnecessary micro-optimization in my opinion.

2 Likes