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)
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
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)
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)