Error with waiting

Hi, I made a module script that will run sounds by queue, but it dies on waiting and does not give an error. I tried to somehow find out exactly where this error comes from and I found it, but I couldn’t solve it in any way.

This is part of the script:

local Requests = {}
local InRunning = false

local DelayBetweenAnns = 1.5

function Run(sound,t)
	task.delay(0.1,function()
		local Notifications = workspace.Audios.Notifications
		local NewSound
		local Found = false

		print("Child running!")

		InRunning=true

		for i,v in pairs(Notifications:GetDescendants()) do
			if v.ClassName=="Sound" then
				if v.Name==sound then
					NewSound=v
					Found=true
					break
				end
			end
		end

		if Found==true then
			if t~=nil then
				if (t):lower()=="warhead" then
					workspace.Audios.Alarm.WarheadAlarm:Play()
					task.wait(4.5)
				elseif (t):lower()=="announcement" then
					workspace.Audios.Notifications.AnnouncementStart:Play()
					task.wait(1.5)
				end
			end
			print("a0")
			NewSound:Play()
			print("a1")
			task.wait((NewSound.TimeLength/NewSound.PlaybackSpeed)+DelayBetweenAnns) --this is part where function is dying
			print("a2")
		else
			warn("Error: The sound '" .. sound .."' does not found in '" .. Notifications:GetFullName() .. "'")
		end

		print("Child ended!")

		table.remove(Requests,1)
		InRunning=false

		if Requests[1] ~= nil then
			Run(unpack(Requests[1]))
		end
	end)
end

And also:
image

For me, this error occurs when another script requests to play different sounds several times, but on the same one sound, module script function dies

Have you tried printing this value above the task.wait call?

local WaitTime = (NewSound.TimeLength/NewSound.PlaybackSpeed)+DelayBetweenAnns

print("Waiting", WaitTime, "seconds")
task.wait(WaitTime)

Also, why are you dividing by PlaybackSpeed??

  1. Yes, it printed me the time, but the function still dying on task.wait()

  2. Because it will depend on how long the sound will play because if you change the value in PlaybackSpeed, the TimeLength will remain unchanged

Well, what did it print? It shouldn’t just stop running forever on task.wait.

it printed a time that matches in sound and does not exceed 20s

Try changing task.wait to wait. Also, does the game freeze after 20s or does the script still exist after 20s?

so the :Play() function doesn’t have a wait so you need to add a wait by your self

task.wait is better than wait changing wont do anything doe

I already had wait() and I thought it was because of it so I changed it to task.wait()

The game does not freeze and the modular and server script (that requests) work, but only after an error, the Run() function can no longer be used because there is a Debounce

well, i didn’t know how to do it

just use

task.wait(sound.TimeLenght)

where sound is your sound

well, it didn’t help with fixing error