Hashtags to check length of table

hello all. Below I have a table which can be defined as currenPlayers, and i’m trying to check if the number of children in the table is equal to 1, if so the game should be over and give the player their wins. And it my function was working until I tested with more than two players, where it threw a ton of errors. I then tried changing it to what it is below where I use GetChildren and find the length of that, but it’s telling me that im trying to call a nil value, even though I am able to print out the entire table and it shows up in the output. Is there something I’m doing wrong with this check or is it something bigger?

if #currentPlayers:GetChildren() == 1 then
		inGame.Value = false
		char.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame + Vector3.new(0,5,0)
		currentPlayers[1].leaderstats.Wins.Value = currentPlayers[1].leaderstats.Wins.Value + 1
			repStorage.gameStatus.Value = currentPlayers[1].Name.." Won!"
		
	end

Did you use a local to get the Area of the Players, then using the #currentPlayers:GetChildren?

what is actually currentPlayers is it table or is it game.Players

what does that me, can you describe it for me?

current players is a table thats made beforehand of all the players when the round starts, so that it doesn’t break when someone joins mid roun

does it store the player names or player instance itself because if it doesnt save the instance then there is no leaderstats

I assume currentPlayers is an array, not an object, therefore the :GetChildren() method doesn’t exist. Instead use the # operator directly on the array:
if #currentPlayers == 1

2 Likes

I orignally had it set to just #currentPlayers

Here is what the output looks like after changing it back to such

Seems like the issue you are having is not removing players from the table correctly when they die. As someone said before, tables are seperate data types than instances so they don’t inherit methods like GetChildren. Using a hashtag operator on a table, specifically an array, will return the size.

no actually. I did some tests, and everytime the table shows up with the correct players, it removes the first player that died correcctly, but as the first picture shows, the same player is dying twice and then for some reason removes a random index from the table. so the problem is that the output is showing the same player dying, when it should be two seperate players

edit: the second picture I posted was incorrect, i messed something up

Can you provide us with the code where you remove players from the table. Also are you clearing the table after each round?

sorry its a little messy, im new to scripting

local repStorage = game.ReplicatedStorage
local inGame = repStorage.inGame
local intermissionTime = 10
local intervalTime = 200
local spinner = game.Workspace.spinner
local Players = game:GetService("Players")
local currentPlayers = {}
local char
local leaderstats
local startPlatform = game.Workspace.startPlatform

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats
end)

local function removeplayers(char)
	local player = Players:GetPlayerFromCharacter(char)
	print(player.Name.." died")
	table.remove(currentPlayers, table.find(currentPlayers, player))
	print(currentPlayers, "Test")

	if #currentPlayers == 1 then
		print(currentPlayers[1].Name.." Won!")
		inGame.Value = false
		char.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame + Vector3.new(0,5,0)
		currentPlayers[1].leaderstats.Wins.Value = currentPlayers[1].leaderstats.Wins.Value + 1
			repStorage.gameStatus.Value = currentPlayers[1].Name.." Won!"
		
	end
end

Players.PlayerRemoving:Connect(removeplayers)

while true do

	wait(5)
	local multi = 1
	if #Players:GetChildren() >= minPlayerCount then
		startPlatform.CanCollide = true
		inGame.Value = true
		currentPlayers = Players:GetChildren()
		for i,v in pairs(Players:GetChildren()) do
			
			v.CharacterRemoving:Connect(removeplayers) --fires the removeplayers function
		
				v.Character.HumanoidRootPart.CFrame = game.Workspace.startPos.CFrame
			
		end
		if inGame.Value == true then
			print(currentPlayers)
			for i = intermissionTime, 1, -1 do
				repStorage.gameStatus.Value = "Time remaining in intermission: "..i
				wait(1)
			end
			startPlatform.CanCollide = false
			repStorage.gameStatus.Value = "Last to survive wins!"
			while inGame.Value == true do
				for i = intervalTime, 1, -1 do
					spinner.CFrame = spinner.CFrame*CFrame.fromEulerAnglesXYZ(0,(0.015*multi),0)
					wait(.01)
				end
				multi = multi*1.2
				end
			end
		end
	end
1 Like

The reason why your code appears to be firing multiple times for one player is most likely because you are connecting this function every loop iteration for all the players. You can fix this by moving this completely out of the while loop. Alternatively you could connect this each round but then you would have to track connections and it seems pretty useles to reconnect this each time. The first solution is probably best.