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 4 years ago
Edited 4 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))

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.

3 answers

Log in to vote
1
Answered by 4 years ago

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

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 — 4y
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 — 4y
0
did this work? please mark it as accepted if it works. royaltoe 5144 — 4y
Ad
Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago
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)


0
It still says 0/-1 marioman1932 48 — 4y
Log in to vote
0
Answered by
BashGuy10 384 Moderation Voter
4 years ago
Edited 4 years ago

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

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

Answer this question