Table.Find Always Returns False for GetAccessories()

Hi, guys. I’m trying to check if a player’s saved items (a dictionary) is on the character currently. However, table.find keeps returning false.

I am aware that table.find only works with arrays which is why I am looping through the dictionary and checking the strings inside to see if it is also found on the player.

--check if wearing something that is already there (accessories)
for itemName, item in pairs (avatarItems) do
	if table.find(player.Character:WaitForChild("Humanoid"):GetAccessories(), itemName) ~= nil then
		print(itemName)
		avatarItems[itemName] = nil
		avatarItems[item]:Destroy()
	end
end

you need to do another for loop for the item variable

for itemName, item in pairs(avatarItems) do
	print(itemName)
	print(item)
	for i, v in item do
		print(i) -- should print the item position in the array
		print(v) -- should print the item
	end
end

edit: I think I got it wrong, I had a brainfart

I believe the problem with this line is that you’re using the index instead of the value itself.

if table.find(player.Character:WaitForChild("Humanoid"):GetAccessories(), item) ~= nil then

Thank you guys for the responses. Sorry, I have to clarify that the “itemName” is the actual accessories’s name and the value(item) is the asset ID.

Are you trying to find a specific accessory that exists or all accessories that are on the character currently?

I’m trying to find all the accessories on the character currently and compare that to the items in the player’s actual datastore (which is a dictionary that looks like this):

[“White Corset”] = “0000”,
[“White Mask”] = “0000”,
[“WhiteBallGown”] = “100042444260688”

Edit: Then delete any items that are copies.

Does avatarItems return a set of each of the accessories names? If it is saved through a dictionary, maybe create a separate table that contains each accessory name by creating a separate for loop?

1 Like

Then I would use the same for loop you used here except you are only using the itemName in this case.

local accessoryNameTable = {}
local accessoryTable = {}
for itemName,item in pairs(avatarItems)
      if itemName ~= nil and type(itemName) == "string" then
         table.insert(accessoryNameTable, itemName)
      end
      if item ~= nil and item:IsA("Accessory") == true then
         table.insert(accessoryTable, item)
      end
end
1 Like
for index, itemName in pairs(accessoryNameTable) do
   if table.find(player.Character:WaitForChild("Humanoid"):GetAccessories(), itemName) ~= nil then
      itemName = nil
   end
end

for index, item in pairs(accessoryTable) do
   if item.AccessoryType ~= nil then
      item:Destroy()
   end
end

The code that I have provided was most likely not right, am aware that it does not delete the instance or set to nil directly.

Edit: Somewhat familiar about the documentation of Roblox accessories, but you would have to get each of the assets and delete them from the original array. Placing each accessory into separate tables was optional, but they already had built in methods :GetAccessories(), :AddAccessories(), and :RemoveAccessories()

Assuming you have already seen this:
https://create.roblox.com/docs/reference/engine/classes/Humanoid#AddAccessory()

" Table.Find Always Returns False for GetAccessories()"
table.find physically cannot do that.
Did you meant table.find()~=nil operation?
Using ipairs/pairs is not needed in Luau by the way.

local HumanoidAccessories:{Accoutrement} = (player.Character:WaitForChild("Humanoid") :: Humanoid):GetAccessories()

It doesnt return Accessory ID it returns an actual instance hence why your script doesnt work in first place.
You are looking for HumanoidDescription

You want to compare names, but table.find searches by exact instance. Try doing this instead:

for _, accessory in player.Character:GetChildren() do
    if (accessory:IsA("Accessory")) then -- optional if-case
        if (avatarItems[accessory.Name]) then 
            -- logic for if accessory exists already goes here
        end
    end
end
1 Like

I actually fixed the error just now and was about to put the solution when I found that you already posted something similar. For me, I checked if the item was of the same type, then removed it from the dictionary using “item.Name” as I found that “item” itself did not remove it. Thank you everyone.

--check if wearing something that is already there
for _, item in ipairs(character:WaitForChild("Humanoid"):GetAccessories()) do --loop through the folder
	if item.AccessoryType == type then
		avatarItems[item.Name] = nil
		item:Destroy()
	end
end

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