Hello everyone. i have this issue where when my script automaticly generates trees/objects.
i coded it so it despawns 600 seconds later when it spawns,
now it despawns less that 10 seconds, anybody know why? here is my code:
local AnimalFolder = game.ServerStorage.Animals
local tree = AnimalFolder.Tree5
local destroyTime = 600
local waitDelay = 0.01
if game.Workspace.Baseplate == nil then
return
end
local newFolder = Instance.new("Folder")
newFolder.Parent = game.Workspace
newFolder.Name = "Workspace.Tree3"
local function CreateNewPart()
local NewTree = tree:Clone()
if not newFolder:IsA("Folder") then
return
end
NewTree.Parent = newFolder
local newCFrame = CFrame.new((math.random((-game.Workspace.Baseplate.Size.X / 2), (game.Workspace.Baseplate.Size.X / 2))), 32.337, (math.random((-game.Workspace.Baseplate.Size.Z / 2), (game.Workspace.Baseplate.Size.Z / 2))))
NewTree:PivotTo(newCFrame)
game.Debris:AddItem(NewTree, destroyTime)
end
local amount = 100
local currentAmount = 0
game.Workspace.ChildAdded:Connect(function(child)
print(child.Name)
end)
newFolder.ChildRemoved:Connect(function(child)
if child.Name == ("Tree5") then
currentAmount = currentAmount - 1
end
end)
while true do
if currentAmount < amount then
currentAmount = currentAmount + 1
CreateNewPart()
print(currentAmount)
end
if currentAmount == amount then
print("Tree spawning is at " .. amount .. ". Currently, Tree aren't spawned anymore till tree is destroyed or deleted.")
end
task.wait(waitDelay)
end