Attempt to index global 'pairs' (a function value)

for i, part in pairs:GetChildren() do
   currentAmount = currentAmount + 1
end

When I execute this code in a script inside workspace, I get the next error in the console:
Workspace.Script:4: attempt to index global ‘pairs’ (a function value)
What does it mean and how can I solve it?

1 Like

pairs is a global iterator function. In your example you are treating it like an Instance. You use it like this:

for key, value in pairs(tbl) do

end

-- to loop directly through an Instance's children..
for i, child in pairs(Instance:GetChildren()) do

end
4 Likes

attempt to call method 'GetChildren' (a nil value)
Now it gives another error…

1 Like

:GetChildren is a method of instances. An instance are things like parts, models, scripts and other stuff. So GetChildren cannot be used on pairs. Your code should look something like:

for i, part in pairs(theNameOfTheModel:GetChildren()) do
   currentAmount = currentAmount + 1
end

This would go through every child inside of the model you have defined. You should also post your updated code so we could see what you’re doing.

1 Like

You need to replace Instance with your reference. My code is an example.

2 Likes

Just specifying what iNoobe_yt wanted to understand what’s wrong

With the pairs, you can’t do:

pairs:GetChildren()

Pairs isn’t an object and it wouldn’t specify what object as it would result in an error.

Try doing…

pairs(OBJECT:GetChildren())
-- OBJECT is the object you are currently dealing with, like:
-- [[ pairs(game.Workspace:GetChildren()) ]]
3 Likes