Auto-Generated Objects Despawn Whilst not wanted

Hello, i have this issue where i created a script that auto spawns objects on top of the Baseplate,
I set the despawn time to 600 seconds ‘10 minutes’ after it spawns. But my Objects despawn way quicker. maybe less than 10 seconds.

Here is my spawn code:

local AnimalFolder = game.ServerStorage.Animals
local tree = AnimalFolder.Tree

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.Tree"

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))), 9.514, (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 = 200
local currentAmount = 0

game.Workspace.ChildAdded:Connect(function(child)
	print(child.Name) 
end)

newFolder.ChildRemoved:Connect(function(child)
	if child.Name == ("Tre") then 
		currentAmount = currentAmount - 1
	end
end)

while true do
	if currentAmount < amount then
		currentAmount = currentAmount + 1
		CreateNewPart()
		print(currentAmount)
	end

	if currentAmount == amount then

	end
	task.wait(waitDelay)
end


If you have a fix, Please say. It’s apreciated.

I usually ignore game.Debris as much as possible. To avoid usng game.Debris, i would usually add a coroutine as so: coroutine.Resume(Coroutine.Create(function(wait(destroyTime) x:Destroy()))

2 Likes

Can you insert the part into my code? I do not know where to put it since im new to coding.

i get error:
Expected Identifier When Parsing Expression, Got )
And
Expected ‘)’ (to close ‘(’ at line 29, Got end.
And
Expected ‘)’ to close collumn 44
and Unknown Global ‘X’

X isn’t a technical thing i implemented, because x is a placeholder, replace it with what you want to be deleted.

1 Like

Just like this?

image

still got some errors.

Pretty much. That should be roughly how i would code it,

But ima rq make it for you.

coroutine.Resume(coroutine.Create(function()wait(destroyTime)bear:Destroy()end))

1 Like

now it wont spawn at all for some reason?

–accidently changed something, works fine.–

I get this error:

Workspace.StoneSpawn:30: attempt to call a nil value

i keep getting the errors. ---------------------

Is it still occuring, I haven’t been looking at this.

You can just simplify it to

task.delay(destroyTime, function()
   x:Destroy()
end)