Failed to load accessory with Asset ID

Im trying to let the player equip the asset they rolled after they rolled the asset but for some reason sometimes it works and sometimes it gives me this error is there a way to solve this?

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local receivedLim = ReplicatedStorage.RecivedLimited
local values = ReplicatedStorage:WaitForChild('SendValues')

local function onPlayerAdded(player)
	local inventory = Instance.new('Folder')
	inventory.Name = player.Name .. " Inventory"
	inventory.Parent = player

	local equippedItems = {} 
	local equipSlotLimit = 3 
	
	local char = player.Character or player.CharacterAdded:Wait()
	local humanoid = char:WaitForChild('Humanoid')
	
	for _, v in ipairs(char:GetChildren()) do
		if v:IsA("Accessory") then
			v:Destroy()
		end
	end

	receivedLim.OnServerEvent:Connect(function(player, assetIds, rap, color, defvalue)
		if #equippedItems < equipSlotLimit then
			if #assetIds >= 5 then
				local fifthAssetId = assetIds[5]

				if not equippedItems[fifthAssetId] then
					local limitedItem = Instance.new('StringValue')
					limitedItem.Name = tostring(fifthAssetId) 
					limitedItem.Value = rap
					limitedItem.Parent = inventory

					values:FireClient(player, color, defvalue)

					task.wait(3.4)
					local success, accessory = pcall(function()
						if #equippedItems < 4 then
						local insertService = game:GetService('InsertService')
						return insertService:LoadAsset(fifthAssetId):FindFirstChildWhichIsA("Accessory")
						end
					end)
					

					if success and accessory then
						local humanoid = player.Character and player.Character:FindFirstChild('Humanoid')
						if humanoid and #humanoid:GetAccessories() < equipSlotLimit then
							local equipSuccess, equipError = pcall(function()
								if #equippedItems < 4 then
								humanoid:AddAccessory(accessory)
								equippedItems[fifthAssetId] = true
								end
							end)
							if not equipSuccess then
								warn("Failed to equip accessory: " .. equipError)
							end
						else
							warn("You can only equip up to " .. equipSlotLimit .. " items.")
						end
					else
						warn("Failed to load accessory with Asset ID: " .. fifthAssetId)
					end
				else
					warn("You already have this item equipped.")
				end
			end
		else
			warn("You can only equip up to " .. equipSlotLimit .. " items.")
		end
	end)
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

Thank you!

1 Like

A pcall is being used here, so usually you’d run the pcall a couple times (until it succeeds) since you’d expect it to error sometimes. If it still hasn’t worked after that, then there’s an actual error with the call.

1 Like

how could i implement this to my script

1 Like
local success, accessory = false
local attemptsLeft = 3
repeat
	success, accessory = pcall(function()
	if #equippedItems < 4 then
		local insertService = game:GetService('InsertService')
		return insertService:LoadAsset(fifthAssetId):FindFirstChildWhichIsA("Accessory")
		end
	end)
	attemptsLeft -= 1
until success or (attemptsLeft < 1)
1 Like

It still didnt works like it works but if you click too fast then it will fail

2 Likes

When you say “click too fast”, what do you mean? By the way, can I see the local script that fires the event?

1 Like

It’s a server script and when I roll over and over again with like no break it like doesn’t load the item and yes each roll has a debouce that will stay for even one second after the roll

1 Like