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!