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)