Coroutine suspends itself instantly?

I have a coroutine with a repeat loop inside that checks the players distance from a part, if humanoid moved, or if the part depletes. Then in my other loop it keeps running until the coroutine is suspended. The problem is the coroutine suspends itself instantly. How do I fix this? I’m new to coroutines.

else

			local mineSound = tool.mineRockSound
			prompt.Enabled = false
			miningAnimationTrack:Play()


			local distanceCheckLoop = coroutine.create(function()
				repeat
					local distance = (humanoidRootPart.Position - prompt.Parent.Position).Magnitude
					local proxDistance = prompt.MaxActivationDistance
					wait()
				until humanoid.MoveDirection.Magnitude > 0 or distance > proxDistance or hpValue.Value <= 0
			end)


			coroutine.resume(distanceCheckLoop)


			while coroutine.status(distanceCheckLoop) == "running" do
				local distance = (humanoidRootPart.Position - prompt.Parent.Position).Magnitude
				local proxDistance = prompt.MaxActivationDistance
				local mineSound = tool.mineRockSound

				task.wait(0.85)
				mineSound:Play()
				task.wait(0.15)
				hpValue.Value -= tool.damage.Value 
				print("did damage")	
			end
		end

or is there a better way to detect that stuff so I can cancel animations/sounds/increments way quicker?

Thanks!

I managed to make it work on my own. Thanks

You should post the solution you found, so others can learn :slight_smile:

1 Like

Hey,
I know this is like months old but I am currently experiencing the same problem.
Could you maybe post the solution as @nicemike40 mentioned?

While I don’t know for sure whether or not this was the solution OP found, calling any yielding/sleep methods such as wait()/task.wait()… will put the coroutine into a suspended state. For a solution, you could try making a variable that says when the distanceCheckLoop is done instead of relying on its thread’s status.

2 Likes

I actually used task.spawn upon recommendation by someone else :sweat_smile:

1 Like