I was making a game, and I made a Number of players chart. I made a script that shows how many players are alive, but didn't a way to tell how many players were alive yet, but that's besides the point. I used the script: ((Local Script))
local players = {} game.Players.PlayerAdded:Connect(function(plr) game.Players:WaitForChild(plr) table.insert(players,#players+1, plr.Name) end) game.Players.PlayerRemoving:Connect(function(plr) game.Players:WaitForChild(plr) table.remove(players, #players) end) print(#players) while wait() do script.Parent.Text = "0".."/"..#players-1 end
And it set the the text to "0/-1" and it printed "0" I also tried to fix it myself by adding:
game.Players:WaitForChild(plr) --and print(#players)
But as you can see, that didn't work. I don't know how to fix it, please help me.
I found it out I just did
game.Players:GetPlayers
and it worked My Code:
while wait() do local players= game.Players:GetPlayers() script.Parent.Text = "0".."/"..#players-1 end
It's still in a local script
local players = {} function removeFromTable(playerName) for i = 1, #players do if(players[i] == playerName)then table.remove(players, i) end end end --print out the players that are in game function printPlayers() local playersString = "Players: " for _,player in pairs(players)do playersString = playersString .. " " .. player end print(playersString) end --add player to the players table game.Players.PlayerAdded:Connect(function(plr) table.insert(players, plr.Name) printPlayers() end) --remove player from players table game.Players.PlayerRemoving:Connect(function(plr) removeFromTable(plr.Name) printPlayers() end)
You should remove the "-1" by #players Line 14
local players = {} game.Players.PlayerAdded:Connect(function(plr) game.Players:WaitForChild(plr) table.insert(players, #players+1, plr) end) game.Players.PlayerRemoving:Connect(function(plr) table.remove(players, #players-1) end) print(#players) while wait() do print(#players+1) -- change this line to what you need make sure it has "#players+1" end
This should work