Something is causing RunService.Heartbeat:Wait() to yield forever, and any other roblox function I try to call in this function

I have a function that gets called upon when I want to run a notification. Due to yielding in that function, I have wrapped it in a coroutine. The function works perfectly fine.

I have another function that plays a sound. Due to yielding in that function, I have wrapped it in a coroutine. The function works perfectly fine.

I call upon the sound function within the notification function. Once the sound function reaches runService.Heatbeat:Wait(), it simply stops. Nothing happens after that. I removed the Heartbeat:Wait(), and now it gets stuck at Sound:Play(), nothing happens.

I found the sound in the explorer during runtime and it shows that “IsLoaded” is true. But the code never completes.

Something is causing the function to either stop running entirely, or hang forever; specifically when I call upon the Heartbeat:Wait() or sound.Loaded:Wait() or sound:Play().

Even if I don’t run the Sound function under a coroutine (as seen in the Gyazo GIF below), it is still getting stuck there. I’m not sure if it matters, but both of these scripts are in a module script that gets required by multiple local scripts.

Here is a video showing the relevant code, and the output.
https://gyazo.com/aa4ee767b47806fb04ab0a41a1c880b4

Edit: If that isn’t enough for you, I can also record a GIF of the code hanging on sound.IsLoaded:Wait() and sound:Play().

Edit2: When I call this function from other functions that don’t have coroutine.wrap, it works as expected. Why is this happening? Is it intended/known behavior? I have not tested yet, but I wonder if spawn() or delay(0, func) will work?

Edit3: Spawn() isn’t working in my game at all now. Doesn’t matter how I do it;
spawn(function() print'hi' end)
or
local f = function() print'hi' end spawn(f)
Either way, it’s not printing at all. I tried it from the module script and from a local script.
Am I experiencing an engine bug? There is no logic behind this at all as far as I can tell. Threading seems to be nonexistent. (Previously, I had no issues with this).

I am unable to pinpoint exactly why this behavior was experienced. It is possible that you are wrapping a coroutine in a coroutine. The previous thread was cut and the other thread never finished in time.

spawn() might have been deprecated?

So why are you running Heartbeat:Wait()?
What a frame would do, why you need it anyway?

1 Like

In a blank baseplate, I just tried running this code in a local script;

local runService = game:GetService'RunService'
coroutine.wrap(function()
	coroutine.wrap(function()
		while runService.Heartbeat:Wait() do
			print'still running'
		end
	end)()
	local i = 3
	while i > 0 do
		i += -1
		print(i)
		runService.Heartbeat:Wait()
	end
	print'coroutine 1 is ending'
end)()

and here is what printed;

  21:50:05.420  2
  21:50:05.451  1
  21:50:05.451  still running (x2)
  21:50:07.173  0
  21:50:07.186  coroutine 1 is ending
  21:50:07.186  still running (x379) -- stopped after 379 because I stopped the test

In this baseplate, spawn() seems to be working fine. I even tried running a spawn within the second spawn (3 threads, each within the next), and the prints went on without interuption.

I use coroutine.wrap in many instances throughout my code framework. Is there a limit to how many threads can be running at once? I doubt that my code is ever running a ridiculous amount of threads at one time, but at this point I’m just throwing out guesses. I definitely think that the thread is being stopped somehow, but I have not added any yields to the coroutine and I don’t know what else could be causing that to happen.

As I said in the OP, even if I remove the Heartbeat:Wait(), it just gets stuck at Sound:Play(). Either way, it shouldn’t be hanging at either of those functions for any reason I can think of.

In this case, can you run a similar code in an empty baseplate with all its other features stripped away? If you do, provide the results.

If it doesn’t work there, it’s possibly a bug. Otherwise, we’ll count on other factors that we didn’t account to it.

I’m not sure why I didn’t realize this sooner, but it just so happens that the local script that calls upon the modulescript that contains the notification function gets deleted shortly after running said function. In doing so, all operations (including those wrapped in coroutine) are ceased.

Lol. Thank you @anon81993163, I appreciate the assistance!

1 Like