Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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

3 comments

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

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

0

did this work? please mark it as accepted if it works.

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)


1 comment

0

It still says 0/-1

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

4 comments

0

if this helped u mark dis as the answer

0

I did print(#players) and it said 0 so if there was 2 players it would say (0/1)

0

editing

0

fixed it

This discussion is preserved as it was. New answers, comments, and votes are closed.