Repeat Loop inside of coroutine not stopping after coroutine.close()

I wish to fix a part of my code that I can’t seem to solve by myself. The issue is that when I run my coroutine and close it early, it does not stop the repeat loop that is inside of said coroutine.
I’ve tried searching up similar problems on the devforum but I didn’t get the answer I wanted and I spent time thinking of a solution but no ideas came to mind.

If I haven’t explained properly, basically the problem is that the code below is inside a coroutine and if I try to stop the coroutine early by using coroutine.close(), the repeat loop does not stop and continues to run until enemies[1] == nil.

repeat
local deltaTime = wait()
for i, v in pairs(enemies) do
	v:SetAttribute("Stunned", v:GetAttribute("Stunned") - deltaTime)
	v.Humanoid.WalkSpeed = 4
	if v:GetAttribute("Stunned") <= 0 then
		table.remove(enemies, table.find(enemies, v))
	end
end
until
enemies[1] == nil

This is not all of the code, it’s only a part of it.

Before you call coroutine.close() , try to remove all the enemies from the table?

Thank you for the idea!
I’ve tried it and it does not work. Most likely because when I call the function stun(arg1, arg2) (function that runs the coroutine with the repeat loop) again, the variables I made in stun(arg1, arg2) aren’t the same variables when I called the function first time.

Basically when I run the function twice, the script doesn’t think the variables from when I ran the function once are the same variables when I ran the function 2nd time even tho they have the same name.
I don’t know a lot about scripting but I think this is what is happening.

I also tried to define the enemies variable outside of the function but it still didn’t work.

I saw this the other day, it says if a coroutine is running you can’t just close it. coroutine | Roblox Creator Documentation

Hi there.
I’m not super familiar with coroutines, but as @Scottifly mentioned, they cannot be closed while running.
What you may be able to do instead is place your code into a Heartbeat loop using
RunService.Heartbeat
That way you can manually disconnect the loop at any time.

Maybe something like this would work for your usecase?

local stunSteppedLoop = nil --To store the RBXScriptConnection object

stunSteppedLoop = game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
	for i, v in pairs(enemies) do
		v:SetAttribute("Stunned", v:GetAttribute("Stunned") - deltaTime)
		v.Humanoid.WalkSpeed = 4
		if v:GetAttribute("Stunned") <= 0 then
			table.remove(enemies, i)
		end
	end
	
	if #enemies <= 0 then --stop the loop once no enemies remain
		stunSteppedLoop:Disconnect()
	end
end)

task.wait(3) --stop the loop manually after 3 seconds

stunSteppedLoop:Disconnect()
1 Like

Thank you @Scottifly for a new fact and @B0B_2 for the current best solution for me! :partying_face:
I was absolutely stuck on coroutines thinking they are the only way to make these 1 frame functions, not even thinking that there is the dedicated RunService to do this job.

Sadly the example you’ve given @B0B_2 did not work, it still somehow stacked the repeat loops on one another where basically 1 second of stun was 0.1 seconds in real time, but the idea was wonderfully executed and it helped me with my problem. I did have to rewrite my stun(arg1, arg2) function for it to work but nonetheless, it worked out the way I wanted it to work out.
One last problem I’m encountering is that the stunSteppedLoop function is ticking faster than it should, where 1 real life second is equal to 2 in-game stun seconds but I’m sure I can find the solution on the DevForum.

1 Like

If that’s happening, trying running stunSteppedLoop:Disconnect() before setting it to a new Heartbeat loop, as the old loop will not disconnect itself.

function stun(arg1, arg2)
	stunSteppedLoop:Disconnect()
	
	--rest of function body here
end

Glad I could help out!

I did. It probably ticks that fast because it goes 60 times a second, where my abomination of a code, before your example, ran 30 times a second. Either way thanks again for your help! :smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.