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))
01 | local players = { } |
02 | game.Players.PlayerAdded:Connect( function (plr) |
03 | game.Players:WaitForChild(plr) |
04 | table.insert(players,#players+ 1 , plr.Name) |
05 | end ) |
06 | game.Players.PlayerRemoving:Connect( function (plr) |
07 | game.Players:WaitForChild(plr) |
08 | table.remove(players, #players) |
09 | end ) |
10 | print (#players) |
11 | while wait() do |
12 | script.Parent.Text = "0" .. "/" ..#players- 1 |
13 | end |
And it set the the text to "0/-1" and it printed "0" I also tried to fix it myself by adding:
1 | game.Players:WaitForChild(plr) |
2 | --and |
3 | 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:
1 | while wait() do |
2 | local players = game.Players:GetPlayers() |
3 | script.Parent.Text = "0" .. "/" ..#players- 1 |
4 | end |
It's still in a local script
01 | local players = { } |
02 |
03 | function removeFromTable(playerName) |
04 | for i = 1 , #players do |
05 | if (players [ i ] = = playerName) then |
06 | table.remove(players, i) |
07 | end |
08 | end |
09 | end |
10 |
11 | --print out the players that are in game |
12 | function printPlayers() |
13 | local playersString = "Players: " |
14 | for _,player in pairs (players) do |
15 | playersString = playersString .. " " .. player |
You should remove the "-1" by #players Line 14
01 | local players = { } |
02 |
03 | game.Players.PlayerAdded:Connect( function (plr) |
04 | game.Players:WaitForChild(plr) |
05 | table.insert(players, #players+ 1 , plr) |
06 | end ) |
07 |
08 | game.Players.PlayerRemoving:Connect( function (plr) |
09 | table.remove(players, #players- 1 ) |
10 | end ) |
11 |
12 | print (#players) |
13 |
14 | while wait() do |
15 | print (#players+ 1 ) -- change this line to what you need make sure it has "#players+1" |
16 | end |
This should work