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

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

Asked by 3 years ago
Edited 3 years ago

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 
0
I dont understand what you mean by #NAMEOFTALE MrGustavio 34 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

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

Ad
Log in to vote
0
Answered by 3 years ago

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

Answer this question