Loop Won't Stop

I want to make this delay stop working when the value of CanWork is changed to false

But whenever the value is changed to false, the system still runs once again

image

CanWork.Changed:Connect(function()
	if CanWork.Value == true then
		local EmpregoFind = Emprego.Value
		local Salario = Turnos.Trabalhos[EmpregoFind].Salario
		while true do
			if CanWork.Value == true then
				delay(DelaySalario,function()
					print("Deleyed")
					Money += Salario[1]
					wait()
					ValorText.Text = "R$ "..tostring(Money)
				end)
				wait()
			else
				break
			end
		end
	end
end)
1 Like

Where are you changing the CanWork value from (server or client)?
Is the code provided from a server or client script?


Also, you should probably do

while CanWork.Value == true

or just

while CanWork.Value

instead of having the if statement and a break statement

Code example
CanWork.Changed:Connect(function()
	if CanWork.Value == true then
		local EmpregoFind = Emprego.Value
		local Salario = Turnos.Trabalhos[EmpregoFind].Salario
		while CanWork.Valuedo
			delay(DelaySalario,function()
				print("Deleyed")
				Money += Salario[1]
				wait()
				ValorText.Text = "R$ "..tostring(Money)
			end)
			wait()
		end
	end
end)
1 Like