For loop doesn't break when told to?

So I have this for loop, and regardless of when I make sure that “blockstopped = true” is called, it still continues to go through the countdown?

Is this another dumb error, or what?

function blockuse()

	script.Parent.RemoteEvent:FireServer("BLOCKING")

	blockanim:Play()

	canattack.Value = false

	for i = blocktime, 0, -1 do

		wait(1)

		print(i)
		
		if blockstopped == true then
			
			break
			
		end

		if i == 0 then

			isblockcooldown = true

			blockstop()
			blockingcooldown()

			break

		end
		
	end

end
1 Like

Right before this line add a print:
print(“blockstopped is”, blockstopped)

And see if that ever prints this in the output: blockstopped is true

1 Like

It doesn’t print. Even though the value is set is another function here:

function blockstop()

	script.Parent.RemoteEvent:FireServer("NOT BLOCKING")

	blockanim:Stop()

	canattack.Value = true
	
	blockstopped = true
	
	wait()
	
	blockstopped = false

end

For context, when the player lets go of R (The block button) it calls “blockstop()” function. So it should stop the countdown before it finishes.

I believe it is because your function, and variable have the same name. So you are checking if the function is true.

If it doesn’t print, you didn’t put the print in the right place. It should print no matter if its true or false.

Print before you check the value:

print(“blockstopped is”, blockstopped)
if blockstopped == true then
1 Like

they arent the same. ones says stopped and one is stop

1 Like

Ah I see, yeah I put it inside the if statement, the print runs normally outside of it because I have it printing the ‘i’ value.

We will need more information, could you post where you change blockstopped to true? Adding --!strict to the top of your script can help catch this bug if it is a simple misspelling issue.

1 Like

Adding --!strict to the top of your script can help catch this bug if it is a simple misspelling issue.

Idk where you got this from. That isn’t a command, especially since there’s a “–” at the beginning which would make it a comment.

I see a couple of problems. Nowhere in your function do I see where blocktime has been defined. What is the value that it starts with? And you don’t need the break with the if i == 0 then block because since 0 will be the last value that i takes before the loop exits, the code is redundant.

1 Like

He/She got it from here:

https://devforum.roblox.com/t/luau-type-checking-release/878947

This is what’s known as a directive. It’s not interpreted as code by the compiler, but it gives instructions to the compiler. Similar to #pragma in gcc/clang-llvm.

2 Likes

I didn’t know that! Really cool feature, I’ll make sure to implement it into my debugging practices.

This ended up being a problem with the wait times, the normal 30 frame period that wait() gives you wasn’t enough to register. Putting a simple 1 second interval into the wait parameters fixed it. Thanks for the help though you guys!

Well, LUA is a new language for me. With that being said, I’ve been using C/C++ and similar languages for something like 25+ years. I find that LUA is…limiting. One glaring omission is the lack of a switch statement.