What do you want to achieve?
Destroy an item inside of the player’s backpack
What is the issue?
The tool inside Player’s backpack just couldn’t be destroyed,
What solutions have you tried so far?
local player=game:GetService("Players")
local playerinstance=player:GetPlayerFromCharacter(script.Parent)
local playercharacter=script.Parent
local playerhumanoid=playercharacter:WaitForChild("Humanoid")
local playerbackpack=playerinstance:WaitForChild("Backpack")
playerbackpack.ChildAdded:Connect(function(newchild)
local newitem=newchild.Name
local existeditems=playerbackpack:GetChildren()
for i = 1, #existeditems do
local items = existeditems[i]
print(items.Name .. " is item number " .. i)
if items.Name==newitem or items.ToolTip==newchild.ToolTip then
local stackamount=newchild:FindFirstChild("Amount")
local stackenabled=newchild:FindFirstChild("StackingEnabled")
if stackamount and stackenabled then
if stackenabled.Value==true then
stackamount.Value+=1
newchild:Destroy()
playerhumanoid:EquipTool(items)
break
end
end
end
end
end)
This script was inside the StarterCharacterScripts
newchild is an item in the first place since it was cloned into the player’s backpack. newchild:Destroy() is what I am trying to do, but it led to the error I mentioned above.
This issue occurs because 2 things affect the parent of an object at the same time.
Try to introduce a wait() and see if it fixes the problem. This has at least fixed a lot of problems for me.