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

How could I fix this? attempt to call method 'GetChildren' (a nil value)

Asked by 9 years ago

I got an error saying:

Workspace.Script:4: attempt to call method 'GetChildren' (a nil value)

19:41:56.037 - Stack Begin

19:41:56.037 - Script 'Workspace.Script', Line 4 - global FindFirstInTable

I don't get why I got this error,why did I get it?

PlayerWithTools = {"Zombomafoo","Player1"}
--------------------------------------------------
function FindFirstInTable(Table,String)
    for _,v in pairs (Table:GetChildren()) do
        if v == String then
            return v,true
        else 
            return false
        end
    end
end
--------------------------------------------------
game.Players.PlayerAdded:connect(function(Player)
    Player.CharacterAdded:connect(function(Character)
----------------------------------------------------
            Player = FindFirstInTable(PlayerWithTools,Player.Name)
                if Player then
                    for _,Tools in pairs (game.ServerStorage:GetChildren()) do
                    ClonedTools = Tools:Clone()
                        ClonedTools.Parent = Player.Backpack
            end
        end
    end)
end)

--Fixed Script--

PlayerWithTools = {"Zombomafoo","Player1"}
--------------------------------------------------
function FindFirstInTable(Table,String)
    for _,v in pairs (Table) do
        if v == String then
            return v,true
        else 
            return false
        end
    end
end
--------------------------------------------------
game.Players.PlayerAdded:connect(function(Player)
    Player.CharacterAdded:connect(function(Character)
----------------------------------------------------
            Player = FindFirstInTable(PlayerWithTools,Player.Name)
                if Player then
                    for _,Tools in pairs (game.ServerStorage:GetChildren()) do
                    ClonedTools = Tools:Clone()
                        ClonedTools.Parent = Player.Backpack
            end
        end
    end)
end)

1 answer

Log in to vote
1
Answered by
User#2 0
9 years ago

Tables do not have a FindFirstChild method by default.

The FindFirstChild method is only a method of objects, and it returns a table.

You're very close, although you have added :GetChildren() where it is not needed.

for _, v in pairs(Table) do
    -- Do what you would like
end
0
Thanks, but now the function won't return  "v"  as the players name why? kevinnight45 550 — 9y
Ad

Answer this question