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

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

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!

1 comment

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)

3 answers

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

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)

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)

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