Why does my loop reiterate after breaking out

Hey guys, I am working on a FNAF-like game and I am trying to make it so the monsters reset after your character dies. (Ignore the name of the monster and model those are just placeholders) I currently am using a while loop which loops until 6 am is reached (if u know fnaf) and when my character dies I just set a value to 6 as the while loop runs while the value is < 6.

The annoying thing is that after the monster is reset, the loop runs again, making it possible for my monster to move. (I make it move every 4 seconds with 100% possibility for testing purposes) I also print out every 4 seconds and after i reset, it prints, even though I think ive exited out of the loop already. I’ve tried making my code simpler, making it neater kinda (im not very neat) and changing how and where the break statement runs.

This is my code:

local resetted = false


rstorage.revents.night1.OnServerEvent:Connect(function()
	local seconds = 0
	rstorage.timer.Value = 0
	
	resetted = false
	
	rstorage.revents.death.OnServerEvent:Connect(function()
		rstorage.timer.Value = 6
		seconds = 0
		task.wait(3)
		reset()
		resetted = true
	end)
	
	while rstorage.timer.Value < 6 do -- runs until 6 am
		--print("loop activated")
		
		if resetted == true then
			break
		end
		
		task.wait(4)

		local hit = 1 --Math.Random(1,3) -- 1 in 3 chance to move hence 1,3
		if hit == 1 then
			moveBigChig()
		else
			print("did not move")
		end

		seconds += 4
		
		--update every minute
		if seconds == 60 then
			rstorage.timer.Value += 1
			seconds = 0
		end
		print(seconds)
	end
end)

(I didn’t put the entire script since this is a server script but rstorage is replicated storage u probably knew that and the move function doesn’t affect the loop)

i appreciate any help as this is my first post lol

The loop’s condition, as well as the check against resetted, are evaluated every 4 seconds. Setting the time 6 or raising the resetted flag will not disrupt the loop if it’s already waiting those 4 seconds. You need to check after waiting, or put the loop into a thread that can be cancelled

so i should put the wait like at the very end of the loop? im kinda lost

while --[[Condition]] do
    if --[[Stop flag]] then
        break
    end

    task.wait(--[[Time]])

    -- Do stuff.
end

This is the flow of your loop. A while loop’s condition is checked before its body is executed. This occurs each iteration. This means your flow is as follows:

  1. Do I continue (while condition)?
  2. Do I continue (if condition)?
  3. Wait
  4. Do stuff

Here, you can see that you check if you should continue iterating twice, and when the loop sees it’s allowed to continue, it will wait, then execute more code. While it’s waiting to execute that code, you change the conditions of the loop to be negative (break-worthy). This does not affect the current iteration, as the loop saw it was allowed to continue, and is now awaiting to execute its final code.

It may appear that your loop is performing another iteration, but it is simply continuing off the current one. It is clear that you need to check the conditions of the loop after waiting, as the conditions can change during that time. Since no other code needs to be executed prior to the wait, you can simply rearrange the code to:

  1. Do I continue? (while condition)
  2. Wait
  3. Do I continue? (if condition)
  4. Do stuff
1 Like

Ohhhh i get it now thank u so much!

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