While Loop not breaking when condition is met, Help is needed!

So my while loop not breaking when if condition is met and I’m really confused to why.

--// Services

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Players = game:GetService("Players")

--// Variables

local RemotesFolder = ReplicatedStorage.Remotes

local CashGiverEvent = RemotesFolder.CashGiverUI

local RobberySound = workspace.AlarmSounds["Jailbreak Robbery Alarm"]

local debounce = false


--// Events

script.Parent.Touched:Connect(function(hit)
	
	    local player = Players:GetPlayerFromCharacter(hit.Parent)
	    local Character = player.Character or player.CharacterAdded:Wait()     
	
	    if player and not debounce then
		    debounce = true
		    RobberySound.Playing = true
		    Character.InVault.Value = true
		    CashGiverEvent:FireClient(player)
		    
		    while true do
			
		    wait(0.25)
		
		    Character.RobbedCash.Value = Character.RobbedCash.Value + 84
		          			
			 if Character.RobbedCash.Value == 1000 then
			        break
		     end
		
	        end
	      wait(.50)
	      debounce = false
	end
	    
end)
1 Like

You might want to try this.

if Character.RobbedCash.Value => 1000 then
      RobbedCash.Value = 1000
      break
end

It might due to the value is over the condition. Which failed to met the condition. This script will automaticly set the value to 1000 in case the value got over the limit.

From what I can see there, your requirement is “== 1000”, which means to break the loop, the Value of RobbedCash must not be more than, must not be less than, must be exactly 1000. However, the interval of added cash is 84 each time. So, here comes the problem. The amount of RobbedCash may be more than 1000, but since it is not “==” (exactly equal to) 1000, so the system considers the requirement not met.

You can try changing the if statement to “>=” or give it a range like the following example.

if Character.RobbedCash.Value >= 1000 and Character.RobbedCash.Value <= 1100 then
 break
end

Let me know if this helps.

1 Like

Well, that didn’t work either. :frowning:

Any errors from the output panel?

Well it is a true so it would run forever

So you would have to do

while debounce == true do

2 Likes

Oh yes, or it will just start the loop again after breaking it.

Same thing, again. (30 characters)

Is the robbed cash parent is the character?

Yes, it is the parent of the character.

Also implemented this.

while debounce == true do
			
		    wait(0.50)
		
		    Character.RobbedCash.Value = Character.RobbedCash.Value + 500
		          			
			 if Character.RobbedCash.Value >= 1000 then
			        debounce = false
		     end
		
	        end
	      wait(.30)
	      debounce = false
	end
	    
end)

Print out the value, also print something inside the if statement that is supposed to break the loop

1 Like

Set the debounce to false then break it

1 Like

Alright, will try that now. (30 chars)

No luck, same thing.

while debounce == true do
			
		    wait(0.50)
		
		    Character.RobbedCash.Value = Character.RobbedCash.Value + 500
		          			
			 if Character.RobbedCash.Value == 1000 then
				    print('true')
			        debounce = false
			        break
		     end
		
	        end
	      wait(.30)
	      debounce = false
	end
	    
end)

It does print true however but doesn’t break it.

You can use this

repeat wait() until Character.RobbedCash.Value >= 1000

Then break it

outside of the if statement if it does not break use return instead

Alright, thank you it works. But why does it stop at 500 though?

How do you know it doesn’t break? Try adding a print after the loop.

It was an equal I changed it though

Keep in mind you are using Milli Seconds so changing it into a second would help

It does break but at 500, not at 1000? Also using a wait of 1 second as well.

Maybe you set the debounce to false at the if statement remove it and then try again