Attempted to index as nil

Working on a sim and struggling to get this local function to work within this script, keeps outputting as nil anyone got any feedback thank you in advance

sorry cant get this script to indent, kinda new here, there more lines of code in this script but this is the part that isn’t working

local function Multiplier(plr, Multiplier1)
local Multi = 0
for i,v in pairs(plr.Pets:GetChildren()) do
if v.Equipped.Value == true then
Multi = Multi + v[Multiplier1].Value
end
end
return Multi
end

local function Multiplier(plr, Multiplier1)
local Multi = 0 -- Multi is Zero
for i,v in pairs(plr.Pets:GetChildren()) do -- indexing a table
if v.Equipped.Value == true then
Multi = Multi + v[Multiplier1].Value -- 0 + ???
end
end
return Multi -- ???
end

Try removing the .Value in v[Multiplier1]

Screenshot 2022-12-15 211406

Do you mind showing the pets table?

Hi Goa7 thank you for the response, sorry I wasn’t clear the output error is showing Pets as nil

Screenshot 2022-12-15 211813

Ok so, looking at your Code, you frogot to add the Arguments inside the function:

local multiplier = Multiplier() -- where are the Arguments???
local function Multiplier(plr, Multiplier1) -- two arguments???
local Multi = 0
for i,v in pairs(plr.Pets:GetChildren()) do
if v.Equipped.Value == true then
Multi = Multi + v[Multiplier1].Value
end
end
return Multi
end

1 Like

Hi, my mind can’t figure this out right now ahaha but still no hope

Use the variable multiplier itself, you might want to change the multiplier variable name in the event scope and multiplier variable name in the global scope.

Hi thank you for your response, sorry please could you expand on this ahaha, I’m still learning about function’s

for i, v in pairs(plr.Pets:GetChildren()) do

You’re looking for “Multiplier1” in the folder “Pets”, which does not exist.
image
Children are one below the tree, descendant links all children of their consecutive children. If that made sense, that is.

1 Like

Totally makes sense, so its getting the children in Pets which would be Pet1, Pet2 etc, would you know how to look for the Multiplier1 in each of the Pets instead to retrieve those values to add in order to get a total multiplier? Sorry may not may sense, I do appreciate the help thank you

You can either use :GetDescendants() or two for loops (not recommended). GetDescendants might not be what you’re looking for.

for _, folder in player:GetChildren() do
    for _, value in folder:GetChildren() do
        if value:IsA("StringValue") then
            print(value.Value)
        end
    end
end

-- or use GetDescendants() to get all descendants of a folder
for _, value in player:GetDescendants() do
    if value:IsA("StringValue") and value.Parent.Name == "FolderName" then
        print(value.Value)
    end
end
1 Like

Believe it or not I finally managed to get it to work, the argument was one of the main factors but after that the error still showed but worked completely fine lol thanks for the help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.