Debris Service being slow/delayed?

I am trying to make a music visualiser where when the music gets to a certain volume, a part is created. I am doing this using debris service, but the destruction of the new part is delayed if too many are created within a small amount of time. I tried fixing this by adding a 0.2s debounce, but this does not fix the problem. Each part created has a 0.5s life time, yet they only live that long if there is 1 or 2 of them. You can see my problem better in this video.

This is the code (made within a localscript, but also made to only work on me)

local ds = game:GetService("Debris")
local ts = game:GetService("TweenService")
local part1 = game.Workspace.Emper0rAwes0me.Part1
local pbl = 0
local sound = game.Workspace.Emper0rAwes0me.HumanoidRootPart.Sound
local debounce = true

local info = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.In,
	0,
	false,
	0
)

local tween = {
	Size = Vector3.new(0.05,0.05,0.05),
	Transparency = 1
}


function trail1()
	local copy1 = part1:Clone()
	copy1.CFrame = part1.CFrame 
	copy1.Parent = part1
	ds:AddItem(copy1,0.5)
	local tweenstuff = ts:Create(copy1,info,tween):Play()
end


while true do wait()
	pbl = sound.PlaybackLoudness / 500
	if pbl >= 0.7 then
		if debounce == true then
			debounce = false
			trail1()
			wait(0.2)
			debounce = true
		end
	end
end

delay(.5,function() Copy1:Destroy() end)

Just a guess, but I would assume debris service uses the wait() function, and for a reason I don’t know, only a certain amount of wait()s can be resumed every resume frame, so it often gets backed up and waits much longer than it really should. This means that @CodeAnomaly’s solution wouldn’t work either, as delay has a wait() built in to it, meaning it will suffer from the same issues as you are currently facing.
Source: Coroutines V.S. Spawn()... Which one should I use? - #10 by buildthomas
Here’s a fix:

local function AddItem(Item, Time)
	coroutine.wrap(function()
		local Start = tick()
		repeat 
			game:GetService("RunService").Heartbeat:Wait()
		until tick() - Start >= Time
		Item:Destroy()
	end)()
end

Hopefully this helps!

2 Likes

Unfortunately, this does not fix the problem. They still do not get destroyed when I need/want them to.