Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

? My table isn't adding values, please help! (SOLVED)

Asked by 5 years ago
Edited 5 years ago

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))

01local players = {}
02game.Players.PlayerAdded:Connect(function(plr)
03    game.Players:WaitForChild(plr)
04    table.insert(players,#players+1, plr.Name)
05end)
06game.Players.PlayerRemoving:Connect(function(plr)
07    game.Players:WaitForChild(plr)
08    table.remove(players, #players)
09end)
10print(#players)
11while wait() do
12    script.Parent.Text = "0".."/"..#players-1
13end

And it set the the text to "0/-1" and it printed "0" I also tried to fix it myself by adding:

1game.Players:WaitForChild(plr)
2--and
3print(#players)

But as you can see, that didn't work. I don't know how to fix it, please help me.

3 answers

Log in to vote
1
Answered by 5 years ago

I found it out I just did

game.Players:GetPlayers

and it worked My Code:

1while wait() do
2    local players= game.Players:GetPlayers()
3    script.Parent.Text = "0".."/"..#players-1
4end   

It's still in a local script

0
alternatively, you can write script.Parent.Text = "0".."/"..#game.Players:GetPlayers(). i thought you wanted a list of the player names earlier so that's why i wrote the script like i did above. my bad. glad you got your script working though royaltoe 5144 — 5y
0
oh okay, also the reason why I keep putting (-1) is because someone is gonna be a killer and it wouldn't make sense to count the killer as a survivor marioman1932 48 — 5y
0
did this work? please mark it as accepted if it works. royaltoe 5144 — 5y
Ad
Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago
01local players = {}
02 
03function removeFromTable(playerName)
04    for i = 1, #players do
05        if(players[i] == playerName)then
06            table.remove(players, i)
07        end
08    end
09end
10 
11--print out the players that are in game
12function printPlayers()
13    local playersString = "Players: "
14    for _,player in pairs(players)do
15        playersString = playersString .. " " .. player
View all 30 lines...
0
It still says 0/-1 marioman1932 48 — 5y
Log in to vote
0
Answered by
BashGuy10 384 Moderation Voter
5 years ago
Edited 5 years ago

You should remove the "-1" by #players Line 14

01local players = {}
02 
03game.Players.PlayerAdded:Connect(function(plr)
04    game.Players:WaitForChild(plr)
05    table.insert(players, #players+1, plr)
06end)
07 
08game.Players.PlayerRemoving:Connect(function(plr)
09    table.remove(players, #players-1)
10end)
11 
12print(#players)
13 
14while wait() do
15    print(#players+1) -- change this line to what you need make sure it has "#players+1"
16end

This should work

0
if this helped u mark dis as the answer BashGuy10 384 — 5y
0
I did print(#players) and it said 0 so if there was 2 players it would say (0/1) marioman1932 48 — 5y
0
editing BashGuy10 384 — 5y
0
fixed it BashGuy10 384 — 5y

Answer this question