Why does my while loop still continue?

I’m currently trying to make a job system, it alerts the player everytime they get paid, I’m trying to make a button where you can resign, it works but for some reason the loop continues and it still alerts the player that they are getting paid. Does anyone know how to fix this?

–Server Script in Server Script Service–

local repStor = game:GetService("ReplicatedStorage")

local isJob = repStor:WaitForChild("Jobs"):WaitForChild("ChangeJob"):FindFirstChild("IsJob")

local changeEvent = repStor:WaitForChild("Jobs"):WaitForChild("ChangeJob")

local resignEvent = repStor:WaitForChild("Jobs"):WaitForChild("Resign")

local hasJob = repStor:WaitForChild("Jobs"):FindFirstChild("HasJob")

local currentJob = repStor:WaitForChild("Jobs"):FindFirstChild("CurrentJob")

local paycheckCooldown = repStor:WaitForChild("Jobs"):WaitForChild("PaycheckCooldown").Value


changeEvent.OnServerEvent:Connect(function(player, jobName, amountPerHour)
	local isJobChange = repStor:WaitForChild("Jobs"):WaitForChild(jobName):WaitForChild("IsJob")
	isJobChange.Value = true
	hasJob.Value = true
	currentJob = jobName
	
end)

game.ReplicatedStorage.Jobs.AddCash.OnServerEvent:Connect(function(player, jobname)
	local cashPerSecond = repStor:WaitForChild("Jobs"):WaitForChild(jobname):WaitForChild("AmountPerHour")
	
	while wait(paycheckCooldown) do
		if hasJob.Value == true then
		player.leaderstats.Cash.Value += cashPerSecond.Value
		game.ReplicatedStorage.Jobs.CashAlert.Value = true
		wait(2)
		game.ReplicatedStorage.Jobs.CashAlert.Value = false
		end
	end
end)

resignEvent.OnServerEvent:Connect(function(player, jobName)
	repStor:WaitForChild("Jobs"):FindFirstChild(jobName).IsJob = false
	hasJob.Value = false
end)```

--Local Script in StarterPlayerScripts--
```lua
local repStor = game:GetService("ReplicatedStorage")
local isJobLP = repStor:WaitForChild("Jobs"):WaitForChild("LitterPicker"):FindFirstChild("IsJob")

local clientEvent = repStor:WaitForChild("Jobs"):FindFirstChild("EnableUI")

local player = game.Players.LocalPlayer

local cashAlertBool = repStor:WaitForChild("Jobs"):WaitForChild("CashAlert")


isJobLP:GetPropertyChangedSignal("Value"):Connect(function()
	if isJobLP.Value == true then
		local jobName = "LitterPicker"
		repStor:WaitForChild("Jobs"):WaitForChild("AddCash"):FireServer(jobName)
	end
end)


cashAlertBool:GetPropertyChangedSignal("Value"):Connect(function()
	if cashAlertBool.Value == true then
		player.PlayerGui.Tips.CashAlert:TweenPosition(UDim2.new(0.422, 0,0, 0),"In","Sine",0.2)
		player.PlayerGui.Tips.CashAlert.Text = "You recieved a paycheck!"
		wait(2)
		player.PlayerGui.Tips.CashAlert:TweenPosition(UDim2.new(0.422, 0,-1, 0),"Out","Sine",0.2)
	end
end)```

--Local Script in apply button--
```lua
local repStor = game:GetService("ReplicatedStorage")

local isJob = repStor:WaitForChild("Jobs"):WaitForChild("LitterPicker"):FindFirstChild("IsJob")

local changeJob = repStor:WaitForChild("Jobs"):WaitForChild("ChangeJob")

local resignEvent = repStor:WaitForChild("Jobs"):WaitForChild("Resign")

local isApply = true
local isResign = false

script.Parent.MouseButton1Down:Connect(function()
	if isJob.Value == false and isApply == true then
		local jobToChange = "LitterPicker"
		local amountPerHour = 7
		changeJob:FireServer(jobToChange, amountPerHour)
		isResign = true
		script.Parent.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
		script.Parent.Text = "RESIGN"
	elseif isResign == true then
		script.Parent.Text = "APPLY"
		script.Parent.BackgroundColor3 = Color3.fromRGB(0, 255,0 )
		local jobName = "LitterPicker"
		resignEvent:FireServer(jobName)
		isApply = true
	end
end)

1 Like

cashAlertBool:GetPropertyChangedSignal(“Value”):Connect(function()
if cashAlertBool.Value == true then
player.PlayerGui.Tips.CashAlert:TweenPosition(UDim2.new(0.422, 0,0, 0),“In”,“Sine”,0.2)
player.PlayerGui.Tips.CashAlert.Text = “You recieved a paycheck!”
wait(2)
player.PlayerGui.Tips.CashAlert:TweenPosition(UDim2.new(0.422, 0,-1, 0),“Out”,“Sine”,0.2)
end
end)
u forgot put cashAlertBool.Value = false after it done tweenposition
but i still dont know where the problem

…Yeah that’s a local script, and I already did it in the server script.

Maybe you can try use new variable for debounce like

local Debounce = false

while wait(paycheckcooldown) do
if Debounce then return end
Debounce = true
– code
Debounce = false
end

If it still dont working i dont know whats the problem

And the question is on

changeEvent.OnServerEvent:Connect(function(player, jobName, amountPerHour)
local isJobChange = repStor:WaitForChild(“Jobs”):WaitForChild(jobName):WaitForChild(“IsJob”)
isJobChange.Value = true
hasJob.Value = true
currentJob = jobName

end)

function(player, jobName, amountPerHour)
Did u forgot put a code for amountPerHour?

And make sure on replicatedstorage paycheckcooldown value has setted the value cooldown or when playeradded it set/add the value again for the paycheck cooldown