Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
1
Answered by 3 years ago

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)
0
This is good, thank you. virushunter9 943 — 3y
Ad

Answer this question