edit: it was an issue with the gun holsters, since the accessory and tool were the exact same name it sometimes deleted the accessory rather than the tool and let the player keep their gun
-
What do you want to achieve? Keep it simple and clear!
I want the upgrade shop to briefly unequip the tool before deleting it, so it can do things like cancel animations or remove any speed buffs before the tool gets deleted, so it doesn’t leave anything behind -
What is the issue? Include screenshots / videos if possible!
Despite calling :Destroy() on the item, it still exists just fine in my inventory. I tried doing print(item.Parent), and returns nil despite it existing fine in my inventory. This allows the player to keep the item they just upgraded while still recieving the new one -
What solutions have you tried so far? Did you look for solutions on the Creator Hub?
waiting for a bit before deleting it in case humanoid:UnequipTools() is somehow pulling it out of the void, it didn’t work
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
Part where it gets deleted
if player.Backpack:FindFirstChild(button.ItemToRemove.Value.Name) then
clearConnectedAccessories(player.Backpack[button.ItemToRemove.Value.Name], player.Character)
player.Backpack[button.ItemToRemove.Value.Name]:Destroy()
else
local item = player.Character:FindFirstChild(button.ItemToRemove.Value.Name)
print(item)
if item ~= nil then
clearConnectedAccessories(item, player.Character)
safeDeleteItem(item, player.Character)
else
warn("could not find the instanced item to remove.")
end
end
Functions
local function safeDeleteItem(item, char)
if char:FindFirstChild("Humanoid") ~= nil then
char.Humanoid:UnequipTools()
task.wait(0.01)
print(item.Name .. " destroyed")
print(item.Parent)
item:Destroy()
print(item.Parent)
task.wait(0.5)
print(item.Parent)
print(player.Backpack:GetChildren())
end
end
-- I don't think this second one should be relevant to the issue but I've left it for clarity
local function clearConnectedAccessories(item, char)
if char == nil then return end
for index, accessory in ipairs(char:GetChildren()) do
if accessory:IsA("Accessory") then
local toolAttachValue = accessory:FindFirstChild("ToolAttachValue")
if toolAttachValue ~= nil then
if toolAttachValue.Value == item then
accessory:Destroy()
end
end
end
end
end
Example with trying to upgrade the DP2A1:
