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

How do I get the number of children in a table?

Output error: ServerScriptService.MainScript:45: attempt to call a nil value

As you can see from the title, I am trying to get the amount of children from a table. At first I tried to see if the table name itself was <= to a number, then I got an error for that. I tried searching it up, and I tried two different things, they were basicly the same thing but the second one used variables. I tried both and got an error, both had the exact same one. The script I am showing was me trying to use the second one. I just need help with figuring out how many children a table has. Tell me if I'm doing something wrong. The line where the error took me is on line 46, I left in the other code just in case it has something to do with it.

-- MAKING TABLE HERE
local PlayerTable = {}
local Platform = game.Workspace.Platform

while true do
        -- NOTHING IMPORTANT HAPPENING YET
	Platform.Touched:Connect(function(hit)
		local Character = hit.Parent
		local Player = game.Players:GetPlayerFromCharacter(Character)
		
		local GameTag = Instance.new("BoolValue")
		GameTag.Parent = Player
		GameTag.Name = "GameTag"
		GameTag.Parent = false
		
		local InRound = false
		
		for _, player in pairs do
			if player == player then
				InRound = true
			end
		end
		
		if not InRound then
			table.insert(PlayerTable, Player.GameTag)
		end
	end)
	
	Platform.TouchEnded:Connect(function(hit)
		local Character = hit.Parent
		local Player = game.Players:GetPlayerFromCharacter(Character)
		
		local InRound = false
		
		table.remove(PlayerTable, Player.GameTag)
	end)
	
	local IntermissionNumber = 15
	
	local IntermissionString = "Intermission for "
	local IntermissionString2 = " more seconds!"
	
	-- IMPORTANT STUFF HERE IN THE WHILE LOOP
	while true do
		
		local Status = game.ServerStorage.Status
		
		-- The error happened below this comment
		local PTChildren = PlayerTable:GetChildren()
		local PTCount = #PTChildren
		
		if PTCount <= 1 then
			Status.Value = "Not Enough Players On Platform to Start Game!"
		end
		
		Status.Value = IntermissionString..IntermissionNumber..IntermissionString2
		
		wait(1)
		IntermissionNumber = IntermissionNumber - 1
		
		if IntermissionNumber <= 0 then
			Status.Value = "Game Starting"
			wait(1)
			break
		end
		
		if PlayerTable <= 1 then
			
		end
	end
end	

1 comment

0

I dont understand what you mean by #NAMEOFTALE

2 answers

To get the number of indexes in an array, you would want to to a # followed by the name of the table. For example:

local variables = {"Hello", "Hi", "Greetings"}
print(#variables)

This would return a value of 3 because there are 3 indexes in the array

So from line 24-26 you made the players to be inserted into the table if they aren't in the round

if not InRound then
	table.insert(PlayerTable, Player.GameTag)
end

But on lines 18-22 the game inserts all the players as inRound so they never get inserted into the table in lines 24-26

for _, player in pairs do
	if player == player then
	InRound = true
	end
end

Thats why the table comes out as nil in line 49

local PTChildren = PlayerTable:GetChildren()

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