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