I’m trying to destroy a tool when it has ran out of resources (ex: when you have 1 berry and you plant it, the berry value will be zero and the tool will be destroyed(Its in another local script that fires a remote event).) But when the tool get destroyed and i picked up another tool/berry , It get cloned and the parent property error will show up. I have a custom backpack(link:How to Make a CUSTOM HOTBAR | HowToRoblox - YouTube)
--Remove tool--
game.ReplicatedStorage.Events.RemoveRescources.OnServerEvent:Connect(function(plr, tool)
tool:Destroy()
end)
--Equip tool--
game.ReplicatedStorage.Events.EquipToolRE.OnServerEvent:Connect(function(plr, tool, parent)
local char = plr.Character
if char then
if parent ~= char then
char.Humanoid:UnequipTools()
char.Humanoid:EquipTool(tool)
else
tool.Parent = plr.Backpack -- The error is here
end
game.ReplicatedStorage.Events.EquipToolRE:FireClient(plr)
end
end)
I tried searching up a solution but it doesnt work. Posted at night
RemoveResources is fired before EquipToolRE which destroys the tool(when destroying a tool the parent gets locked to NULL), then the same tool(I assume from a reference on the client?) gets passed to EquipToolRE which attempts to change its parrent, causing the error.
To avoid the error, you have to clone the object before destroying it, and manage the cloned version inside EquipToolRE.
Basically create a clone of the tool(which can be stored in a variable or in a service for example ServerStorage) and inside EquipToolRE make the client send the name of the tool instead(toolName). After you sent the name retrieve the tool by doing local tool = game.ServerStorage[toolName]:Clone() and manage that instead.