How to insert all players into table upon joining

So i’m trying to make a table for players who are alive and put all players into it upon them joining the game and remove them when they are “dead” but i don’t know how to put them into the table in the first place.

Thanks for your help in advance!

This should add the player to a table when they join the and then remove them from the table once they die.

local Players = game:GetService("Players")

local PlayerTable = {}

local function PlayerAdded(Player)
	table.insert(PlayerTable, Player)
	print("Added")
	
	local function CharacterAdded(Character)
		local CharacterCoroutine = coroutine.wrap(function()
			if not Character:IsDescendantOf(workspace) then
				repeat
					task.wait(0.5)
				until Character:IsDescendantOf(workspace)
			end

			local Humanoid = Character:WaitForChild("Humanoid")

			while Humanoid do
				if Humanoid.Health <= 0 then
					break
				end
				
				task.wait(0.5)
			end
			
			local Index = table.find(PlayerTable, Player)
			if Index then
				table.remove(PlayerTable, Index)
				print("Removed")
			end
		end)
		
		CharacterCoroutine()
	end
	
	Player.CharacterAdded:Connect(CharacterAdded)
end

Players.PlayerAdded:Connect(PlayerAdded)
3 Likes

Sorry if this sounds dumb. But what does the coroutine part of the code from

Character Added 

To

repeat 

until

actually do?

It just waits until the character is added to the workspace before doing anything else.

1 Like