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

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

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

✓ Accepted answer

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

1 comment

0

Thanks, but now the function won't return  "v"  as the players name why?

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