Stocks code for my game wont work

This is the code so when the button is clicked it changes the Boolean to true and when the sell button is clicked the Boolean is changed to false but the script keeps giving money even though the Boolean is false.

game.ReplicatedStorage.RES.DS.OnServerEvent:Connect(function(player)
	game.ReplicatedStorage.StockChecks.DonutCheck.Value = true
	while true do
		if game.ReplicatedStorage.StockChecks.DonutCheck.Value == true then
			player.leaderstats.Money.Value = player.leaderstats.Money.Value + 10
			wait(5)
		else
			print("Stoped The Stocks")
		end
	end
end)

game.ReplicatedStorage.RES.CM.OnServerEvent:Connect(function(player)
	game.ReplicatedStorage.StockChecks.CarCheck.Value = true
	while true do
		if game.ReplicatedStorage.StockChecks.CarCheck.Value == true then
			player.leaderstats.Money.Value = player.leaderstats.Money.Value + 100
			wait(5)
		else
			print("Stoped The Stocks")
		end
	end
end)

game.ReplicatedStorage.RES.GD.OnServerEvent:Connect(function(player)
	game.ReplicatedStorage.StockChecks.GameCheck.Value = true
	while true do
		if game.ReplicatedStorage.StockChecks.GameCheck.Value == true then
			player.leaderstats.Money.Value = player.leaderstats.Money.Value + 1000
			wait(5)
		else
			print("Stoped The Stocks")
		end
	end
end)


Your condition for each of your loops are always true. Furthermore, this code can cause severe memory leaks and slow performance if the loops are not handled correctly.

Insert break below the print statements to stop the loops from running

else
    print("Stopped the stocks")
    break
end

I don’t think they want to break out of the loop, I think they just want it to pause and stop giving money while the boolean is false, but immediately start back up again when the boolean is true.

else
    print("Stopped the stocks")
    repeat wait() until game.ReplicatedStorage.StockChecks.GameCheck.Value == true
end

But he should also make sure to break out of those while loops when a player leaves.

I’m not sure this code you’re giving is enough to solve your problem. Are you sure the boolean is changed to false? Add a print statement printing the the DonutCheck.Value at the start in your while loop and see what you are getting.