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

Why do I get this error?: "attempt to index nil with 'GetDescendants'"

Asked by
mcox6 5
3 years ago

Right now I am attempting to get every new player's acessory name and put it into a table. I run into an issue when I run this code to print the namer of every acessory on a player

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    print(player.Name .. " joined the game!")
    print(player.Name .. " joined the game!")
    local char = player.Character or player.CharacterAdded:Wait()
    print(char)
    print(player)
    print(player.Character)
    print("Test Test TEst")
    local function Getacessory(char)
        local descendants = char:GetDescendants()
        for index, descendant in pairs(descendants) do
            if descendant:IsA("Accessory") then
                print(descendant.Name)
            end

        end
    end
Getacessory()
end)

If anyone has solutions for a more efficient technique or a solution pls let me know!

0
the parameter "char" in your function isn't the character because when you call the function on line 20, the character isn't defined in those parenthesis. Try changing line 20 to Getacessory(char) greatneil80 2647 — 3y

3 answers

Log in to vote
0
Answered by 3 years ago

I think tables might require brackets like this {} but not sure whats wrong.

Ad
Log in to vote
0
Answered by 3 years ago

Would recommend not doing

local char = player.Character or player.CharacterAdded:Wait()

Instead do

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    print(player.Name .. " joined the game!")
    print(player.Name .. " joined the game!")
    player.CharacterAdded:Connect(function(char)
        print(char)
        print(player)
        print(player.Character)
        print("Test Test TEst")
        local function Getacessory(char)
            local descendants = char:GetDescendants()
            for index, descendant in pairs(descendants) do
                if descendant:IsA("Accessory") then
                    print(descendant.Name)
                end
            end
        end
        Getacessory()
    end)
end)
Log in to vote
0
Answered by
abrobot 44
3 years ago

I think its because you told the function to except a parameter Char, but when you fired etacessory() you didn't send a parameter and because of the scope,it trys to use the char that was passed to the function not the one outside of it, but you never actually passed anything to char when you fired Getacessory() so char is nill.

i think you can just remove the parameter when making the function

so instead of this its:

local function Getacessory(char)

its this:

local function Getacessory()

or you could pass char when calling the function like this:

Getacessory(Char)

Answer this question