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
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):
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?
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
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()
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
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