I have a system where if an item(default roblox tool) is not picked up after it's despawn time, it destroys itself and is despawned, however if it is picked up by a player, it is counted as "PickedUp" and will no longer despawn. Since this is my first time doing something like this, I was wondering if there is a more effecient way to write this code.
--spawning stuff(just an example) local ToolClone = Tool:Clone() ToolClone.Parent = game.Workspace spawn(function() local PickedUp = false ToolClone.AncestryChanged:Connect(function(child, parent) if parent:FindFirstChild("Humanoid") or ToolClone:IsDescendantOf(game.Players) then PickedUp = true end end) wait(300) if ToolClone then if PickedUp = false then ToolClone:Destroy() end end end)
It looks good, but line 17 should be:
if PickedUp == false then
A more simple way would be:
spawn(function() local ToolClone = Tool:Clone() ToolClone.Parent = game.Workspace wait(300) if not ToolClone.Parent:FindFirstChild("Humanoid") and not ToolClone:IsDescendantOf(game.Players) then ToolClone:Destroy() end end)