Implement Time-Limited Despawn for Player-Placed Flares and Ladders
please make flares and ladders despawn after a time limit, I made the code for you to save yall some time as i know yall are busy.
local CollectionService = game:GetService("CollectionService")
local CLEANUP_SECONDS = 20 * 60
local PLAYER_PLACED_TAG = "PlayerPlaced"
local PLACED_ROOT: Instance? = workspace:FindFirstChild("PlacedObjects")
local ALLOWED = {
["Flare"] = true,
["Ladder"] = true,
}
local function isAllowed(inst: Instance): boolean
if not CollectionService:HasTag(inst, PLAYER_PLACED_TAG) then
return false
end
if PLACED_ROOT and not inst:IsDescendantOf(PLACED_ROOT) then
return false
end
if ALLOWED[inst.Name] then
return true
end
local placedType = inst:GetAttribute("PlacedType")
if typeof(placedType) == "string" and ALLOWED[placedType] then
return true
end
return false
end
local function scheduleCleanup(inst: Instance)
if inst:GetAttribute("PlacedAtUnix") == nil then
inst:SetAttribute("PlacedAtUnix", os.time())
end
task.delay(CLEANUP_SECONDS, function()
if not inst or inst.Parent == nil then
return
end
if isAllowed(inst) then
inst:Destroy()
end
end)
end
for _, inst in ipairs(CollectionService:GetTagged(PLAYER_PLACED_TAG)) do
if isAllowed(inst) then
scheduleCleanup(inst)
end
end
CollectionService:GetInstanceAddedSignal(PLAYER_PLACED_TAG):Connect(function(inst: Instance)
if isAllowed(inst) then
scheduleCleanup(inst)
end
end)
Its Lua Cheers!