player.CharacterAdded not adding player to table

game.Players.PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function()
        table.insert(playersInTable, player)
		print("Added player to table")
end)
end)

I am trying to add player to table but it use to work but doesn’t anymore, any help is appreciated

Full script:

wait(1.5)

local Players = game:GetService("Players")
local plrs = game.Players:GetPlayers()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")
local TweenService = game:GetService("TweenService")


local IntermissionEvent = script.Parent.Intermission
local StartGameEvent = script.Parent.StartGame

local ValuesFolder = ReplicatedStorage:WaitForChild("Values")
local Status = ValuesFolder:WaitForChild("Status")
local InIntermission = ValuesFolder:WaitForChild("InIntermission")
local inGame = ValuesFolder:WaitForChild("InGame")
local TeleportTeamPlayers = script.Parent.TeleportTeamPlayers
local NotEnoughPlayersEvent = script.Parent.NotEnoughPlayers

local HomeScore = ValuesFolder:WaitForChild("HomeTeamScore")
local AwayScore = ValuesFolder:WaitForChild("AwayTeamScore")

local HomeSpawn = game.Workspace:WaitForChild("Home")
local AwaySpawn = game.Workspace:WaitForChild("Away")

local Teams = game:GetService("Teams")

local HomeGoalTrigger = game.Workspace:WaitForChild("HomeGoalTrigger")
local AwayGoalTrigger = game.Workspace:WaitForChild("AwayGoalTrigger")
--local Ball = game.Workspace.TPS

local KickOffPosition = Vector3.new(219.574, 2.502, -437.348)

local NewBall = game.Lighting:WaitForChild("TPS")
local StartGameFired = false
local IntermissionFired = false
local CheckAmountOfPlayersFired = false

local TPS = NewBall:clone()

local playersInTable = {}

local function CheckAmountOfPlayers()
	repeat
		print("Repeated")
		local _Players = Players:GetPlayers()
		print(_Players)
		if #_Players < 2 then
			script.Parent.NotEnoughPlayers:Fire()
			print("Fired")
			Players.PlayerAdded:Wait()
		end

	until #_Players > 1
end

script.Parent.CheckAmountOfPlayers.Event:Connect(CheckAmountOfPlayers)

local function GiveTeamWinsOnPlayerLeaving(TeamName,Amount)
	for _,player in pairs(game.Players:GetChildren()) do
		if player.Team.Name == TeamName then
			print(player.Team.Name .. " Has won the game")
			player.stats.Wins.Value += Amount
			print("Gave player win")
			break
		end
	end
end

local function GiveTeamWins(TeamName,Amount)
	for _,player in pairs(game.Players:GetChildren()) do
		if player.Team.Name == TeamName then
			print(player.Team.Name .. " Has won the game")
			player.stats.Wins.Value += Amount
			print("Gave player win")
		end
	end
end

local function GiveTeamGoals(TeamName,Amount)
	for _,player in pairs(game.Players:GetChildren()) do
		if player.Team.Name == TeamName then
			print(player.Team.Name .. " Has scored")
			player.stats.Goals.Value += Amount
			print("Gave player goal")
		end
	end
end

local function StartingGame()

	script.Parent.TeleportTeamPlayers:Fire()
	print("Starting Game")
	if not game.Workspace:FindFirstChild("TPS") then
		local TPS = game.Lighting:WaitForChild("TPS"):Clone()
		TPS.Parent = workspace
		TPS.Position = KickOffPosition
		TPS.AssemblyAngularVelocity = Vector3.new(0, 0, 0)
		TPS.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
	end
	--Ball.Position = Vector3.new(221.21, 1.944, -437.77)
	--Ball.AssemblyAngularVelocity = Vector3.new(0, 0, 0)
	--Ball.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
	HomeGoalTrigger.Parent = workspace
	AwayGoalTrigger.Parent = workspace
	inGame.Value = true
	InIntermission.Value = false
	Status.Value = "In match"

	script.Parent.CheckAmountOfPlayers:Fire()
	CheckAmountOfPlayersFired = false
	
	while true do
		task.wait()
		if inGame.Value == false then
			TPS:Destroy()
			break
		elseif HomeScore.Value == 5 then -- If home has scored 3 goals
			GiveTeamWins("Green", 1)
			Status.Value = "Green team wins the game"
			print("5 goals have been scored")
			wait(2)
			inGame.Value = false
			StartGameFired = false
			if not IntermissionFired then
				IntermissionFired = true
				script.Parent.Intermission:Fire()
			else
				print("Intermission is already fired")
			end
			break
		elseif AwayScore.Value == 5 then
			GiveTeamWins("Red", 1)
			Status.Value = "Red wins the game"
			print("5 goals have been scored")
			wait(2)
			inGame.Value = false
			StartGameFired = false
			if not IntermissionFired then
				IntermissionFired = true
				script.Parent.Intermission:Fire()
			else
				print("Intermission is already fired")
			end
			break
		end
	end
		script.Parent.CheckAmountOfPlayers:Fire()
	StartGameFired = false
	inGame.Value = false
end

script.Parent.StartGame.Event:Connect(StartingGame)

local function Intermission()
	for _, player in pairs(playersInTable) do
		player.Team = game.Teams.Selecting
		print("Players new team is selecting")
	end
	print("Fired intermission function")
	HomeScore.Value = 0
	AwayScore.Value = 0
	HomeGoalTrigger.Parent = game.ServerStorage
	AwayGoalTrigger.Parent = game.ServerStorage
	inGame.Value = false
	InIntermission.Value = true
	for i = 5, 0, -1 do
		Status.Value = "Intermission: " .. i
		CheckAmountOfPlayers()
			print("Fired CheckPlayersAmount Event")
		end
		task.wait(1)
	end
	print(#playersInTable)
	
	local chosenGreen = playersInTable[math.random(1, #playersInTable)]
	print(chosenGreen)
	if #game.Teams.Green:GetPlayers() < 1 then
		chosenGreen.Team = game.Teams.Green
		print("There is no players in away therefore the players team is Green")
	elseif #game.Teams.Green:GetPlayers() == 1 then
		chosenGreen.Team = game.Teams.Red
		print("There was too many players in Green so players team is Red")
	end

	for _, player in pairs(game.Teams.Selecting:GetPlayers()) do
		if player.Team ~= game.Teams.Green then
			local success, err = pcall(function()
				player.Team = game.Teams.Red
				print("Team changed to red")
			end)
		end	

	inGame.Value = true
	IntermissionFired = false

	print(StartGameFired)
	if not StartGameFired then
		StartGameFired = true
		script.Parent.StartGame:Fire()
	end
end

script.Parent.Intermission.Event:Connect(Intermission)

local function SendPlayersToSpawns()
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player and player.Character then
			if player.Team == game.Teams.Green then
				player.Character:MoveTo(HomeSpawn.Position)
				print("Teleported " .. player.Name .. " to team spawn")
			end
			if player.Team == game.Teams.Red then
				player.Character:MoveTo(AwaySpawn.Position)
				print("Teleported " .. player.Name .. " to team spawn")
			end
		end
	end
end

script.Parent.TeleportTeamPlayers.Event:Connect(SendPlayersToSpawns)

local function NotEnoughPlayers()
	Status.Value = "Need 2 players to start the game."
	HomeGoalTrigger.Parent = game.ServerStorage
	AwayGoalTrigger.Parent = game.ServerStorage
end

NotEnoughPlayersEvent.Event:Connect(NotEnoughPlayers)

--repeat
--print("Repeated")
--local _Players = Players:GetPlayers()
--print(_Players)
--if #_Players < 2 then
--script.Parent.NotEnoughPlayers:Fire()
--print("Fired")
--Players.PlayerAdded:Wait()
--end

--until #_Players > 1

--if #plrs == 2 and InIntermission.Value == true then

--else
--script.Parent.NotEnoughPlayers:Fire()
--end

--if #plrs == 2 and inGame.Value == true then
--	print(#plrs)
--script.Parent.StartGame:Fire()
--	script.Parent.TeleportTeamPlayers:Fire()
-- end

if InIntermission.Value == true then
	if not IntermissionFired then
		IntermissionFired = true
		print("In intermission")
		script.Parent.Intermission:Fire()
	else
		print("Intermission is already fired")
	end
end

if inGame.Value == true then
	print(#playersInTable)
	if not StartGameFired then
		StartGameFired = true
		script.Parent.StartGame:Fire()
		script.Parent.TeleportTeamPlayers:Fire()
	else
		print("StartGame is already fired")
	end
end

HomeGoalTrigger.Touched:Connect(function(hit)
	if hit.Name == "TPS" then
		script.Parent.TeleportTeamPlayers:Fire()
		for index,v in pairs(playersInTable) do
			print("In loop")
			if v == hit.Owner.Value then
				if v.Team == game.Teams.Green then
					print("Own goal")
					ValuesFolder:WaitForChild("OwnGoal").Value = true
				elseif v.Team == game.Teams.Red then
					print("Normal goal")
					GiveTeamGoals("Red", 1)
				end
			end
		end
		task.wait(0.25)

		AwayScore.Value = AwayScore.Value + 1
		ValuesFolder:WaitForChild("OwnGoal").Value = false
		hit:Destroy()
		local TPS = game.Lighting:WaitForChild("TPS"):Clone()
		TPS.Parent = workspace
		TPS.Position = KickOffPosition
		TPS.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
		TPS.AssemblyAngularVelocity = Vector3.new(0, 0, 0)
	end
end)

AwayGoalTrigger.Touched:Connect(function(hit)
	if hit.Name == "TPS" then
		script.Parent.TeleportTeamPlayers:Fire()
		for index,v in pairs(playersInTable) do
			if v == hit.Owner.Value then
				if v.Team == game.Teams.Red then
					print("Own goal")
					ValuesFolder:WaitForChild("OwnGoal").Value = true
				elseif v.Team == game.Teams.Green then
					print("Normal goal")
					GiveTeamGoals("Green", 1)
				end
			end
		end
		task.wait(0.25)
		HomeScore.Value = HomeScore.Value + 1
		ValuesFolder:WaitForChild("OwnGoal").Value = false
		hit:Destroy()
		if HomeScore.Value < 5 then
			local TPS = game.Lighting:WaitForChild("TPS"):Clone()
			TPS.Parent = workspace
			TPS.Position = KickOffPosition
			TPS.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
			TPS.AssemblyAngularVelocity = Vector3.new(0, 0, 0)
		end
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	table.insert(playersInTable, player.Name)
	print("Added player to table")
end)


game.Players.PlayerRemoving:Connect(function(Plrs)
	print("Player has left the game data has been removed.")
	if Plrs.Team == game.Teams.Red and inGame.Value == true and AwayScore.Value > HomeScore.Value then
			script.Parent.CheckAmountOfPlayers:Fire()
			print("Fired CheckPlayersAmount Event")
		print("Fired as there is not enough players")
		if not IntermissionFired then
			IntermissionFired = true
			script.Parent.Intermission:Fire()
			print("Fired CheckPlayersAmount Event")
			StartGameFired = false
		else
			print("Intermission is already Fired")
		end

	elseif Plrs.Team == game.Teams.Red and inGame.Value == true and AwayScore.Value <= HomeScore.Value then

			script.Parent.CheckAmountOfPlayers:Fire()
			print("Fired CheckPlayersAmount Event")
			print("Fired as there is not enough players")
		if not IntermissionFired then
			IntermissionFired = true
			script.Parent.Intermission:Fire()
			StartGameFired = false
		else
			print("Intermission is already Fired")
		end
		GiveTeamWinsOnPlayerLeaving("Green", 1)
	end
	if Plrs.Team == game.Teams.Red and inGame.Value == true and HomeScore.Value > AwayScore.Value then
			script.Parent.CheckAmountOfPlayers:Fire()
			print("Fired CheckPlayersAmount Event")
			print("Fired as there is not enough players")
		if not IntermissionFired then
			IntermissionFired = true
			script.Parent.Intermission:Fire()
			StartGameFired = false
		else
			print("Intermission is already Fired")
		end

	elseif Plrs.Team == game.Teams.Green and inGame.Value == true and HomeScore.Value <= AwayScore.Value then
			script.Parent.CheckAmountOfPlayers:Fire()
			print("Fired CheckPlayersAmount Event")
		print("Fired as there is not enough players")
		if not IntermissionFired then
			IntermissionFired = true
			script.Parent.Intermission:Fire()
			StartGameFired = false
		else
			print("Intermission is already Fired")
		end
		GiveTeamWinsOnPlayerLeaving("Red", 1)
	end
	
	Plrs.CharacterRemoving:Connect(function()
		for index,v in pairs(playersInTable) do
			if v == Plrs then
				table.remove(playersInTable, index)
				print("Player has been removed from table")
			end
		end
	end)
end)

Is there any error? Also, I am unsure if you can add the player instance to a table. Maybe you should add their username or their user ID.

Do this instead.

game.Players.PlayerAdded:Connect(function(player)
        table.insert(playersInTable, player.Name)
		print("Added player to table")
end)

Only gets an error when its randomising players since there is no players in table was working don’t know what happened

Still not working It could be something with my script but its pretty long

You can add a player to the table, although finding it through table.find() would require you to find it via the player instance itself example: table.find(players, game.Players['Player 1'])

to the OP: Try printing the table once in a while and see if it updates properly or not so it’d be easier to figure out or narrow down the issue.

When I start the game I have a print(#playersInTable) which outputs “0”

I might just revert possibly could fix it because it was working before

It could also be caused by the wait(1.5) perhaps? Just tested it and that seems to be the issue for me.
image
image
image


Nothing in second case.

I recommend pushing the adding function before any wait()s and whatnot (I’m also assuming this is a server script.)

1 Like

If it’s not a serverScript, you should add a loop when the script first runs to see what players are in already.
This could also fix the wait() issue.

The problem comes from the wait. In studio your client will join before you connect to the PlayerAdded event. You should connect to the player added event before all the rest code.

local playersInTable = {};

game.Players.PlayerAdded:Connect(function(player)
	table.insert(playersInTable, player.Name)
	print("Added player to table");
end)
--//Could add the player removing event here aswell

--> Rest of your code 


1 Like