Either you need to use multi-thread functions, like a coroutine or spawn, or use the debris service.
Using the Debris service.
function MakePart()
local Part = Instance.new("Part",game.Workspace)
Part.Size = Vector3.new(math.random(1,10),math.random(1,10),math.random(1,10))
Part.Position = Vector3.new((math.random(-50,50)),50,(math.random(-50,50)))
Part.Color = Color3.new(math.random(),math.random(),math.random())
return Part
end
while true do
wait(.3)
local p = MakePart()
game:GetService("Debris"):AddItem(p, 2)
end
Using spawn.
function MakePart()
local Part = Instance.new("Part",game.Workspace)
Part.Size = Vector3.new(math.random(1,10),math.random(1,10),math.random(1,10))
Part.Position = Vector3.new((math.random(-50,50)),50,(math.random(-50,50)))
Part.Color = Color3.new(math.random(),math.random(),math.random())
return Part
end
function DestroyItem(Instance, Time)
wait(Time)
if Instance then
Instance:Destroy()
end
end
while true do
wait(.3)
local p = MakePart()
spawn(function()
DestroyItem(p, 2)
end)
end
I'm kind of in favor with the spawn function, because according to "Notes" in this link, the AddItem function in the Debris service removes instances like the Remove method does (a deprecated method of removing things).
1 comment
Thanks so much!