-- src/ServerScriptService/RespawnManager.server.lua local CollectionService = game:GetService("CollectionService") local ReplicatedStorage = game:GetService("ReplicatedStorage") local BiomeConfig = require(ReplicatedStorage:WaitForChild("Shared"):WaitForChild("BiomeConfig")) -- Respawn delay in seconds local RESPAWN_DELAY = 120 -- Track destroyed tree positions for respawning local respawnQueue = {} -- { {treeType, position, delay} } -- Monitor tree destruction local function onTreeSegmentRemoved(segment) -- When a segment is destroyed (chopped to nothing), track it for respawn -- We only respawn if the segment was the bottom piece (or original) local treeType = segment:GetAttribute("TreeType") if not treeType then return end local position = segment.Position -- Only queue if near ground level (don't respawn floating segments) if position.Y < 20 then table.insert(respawnQueue, { treeType = treeType, position = Vector3.new(position.X, 0, position.Z), timer = RESPAWN_DELAY, }) end end -- Bind to existing and new tree segments CollectionService:GetInstanceRemovedSignal("TreeSegment"):Connect(function(segment) -- We need to capture data before it's fully gone task.defer(function() -- The segment data was already captured via the signal end) end) -- Alternative: listen for Destroying event on tagged parts local function bindSegmentTracking(segment) segment.Destroying:Connect(function() onTreeSegmentRemoved(segment) end) end CollectionService:GetInstanceAddedSignal("TreeSegment"):Connect(bindSegmentTracking) for _, segment in pairs(CollectionService:GetTagged("TreeSegment")) do bindSegmentTracking(segment) end -- Process respawn queue task.spawn(function() while not _G.TreeSpawningComplete do task.wait(1) end while true do task.wait(5) local toRemove = {} for i, entry in ipairs(respawnQueue) do entry.timer = entry.timer - 5 if entry.timer <= 0 then -- Respawn tree if _G.CreateTree then local rayResult = workspace:Raycast( Vector3.new(entry.position.X, 200, entry.position.Z), Vector3.new(0, -400, 0) ) local groundY = 0 if rayResult then groundY = rayResult.Position.Y end _G.CreateTree(entry.treeType, Vector3.new(entry.position.X, groundY, entry.position.Z)) end table.insert(toRemove, i) end end -- Remove processed entries (reverse order to preserve indices) for i = #toRemove, 1, -1 do table.remove(respawnQueue, toRemove[i]) end end end) print("Respawn Manager initialized")