File size: 2,649 Bytes
0712d5f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | -- 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")
|