Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there a better way to write this item despawn code?

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)

1 answer

✓ Accepted answer

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)

1 comment

0

This is good, thank you.

This discussion is preserved as it was. New answers, comments, and votes are closed.