Remote Event to break minsAfterMidnight Loop

Currently having a problem where a button from a client side GUI is meant to stop a day/night cycle through the server with a remote event.

-- Client

local Button = script.Parent
ButtonTrue = false
Debounce = true

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage.TimeCycle




function ButtonOff ()
	-- When the button is turned off.
	remoteEvent:FireServer()
	print("Button Turned Off")
end

function ButtonOn ()
	-- When the button is turned on.
	remoteEvent:FireServer()
	print("Button Turned On")
end



if Debounce then
	Debounce = false
	Button.MouseButton1Down:Connect(function()
		if ButtonTrue == false then
			ButtonTrue = true
			Button.Parent.Button:TweenPosition(UDim2.new(0, 14,0, 13), "Out", "Linear", .1)
			Button.Parent.BackgroundColor3 = Color3.new(0.8, 0.8, 0.8)
			ButtonOff()
		else
			ButtonTrue = false
			Button.Parent.Button:TweenPosition(UDim2.new(0, 25,0, 13), "Out", "Linear", .1)
			Button.Parent.BackgroundColor3 = Color3.new(0, 0.792157, 0.305882)
			ButtonOn()
		end
	end)
	wait(Debounce)
	Debounce = true
end
-- Server
local lighting = game:GetService("Lighting")
local minsAfterMidnight = 0
local daylength = 0.1

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage.TimeCycle
pause = false

remoteEvent.OnServerEvent:Connect(function()
	if pause == false then
		pause = true
	else
		pause = false
	end
end)

while true do
	wait()
	if pause == true then
		print("Cycle Stopped.")
	else
		while true do
			minsAfterMidnight = minsAfterMidnight + 1
			local minutesNormalised = minsAfterMidnight % (60 * 24)
			local hours = minutesNormalised / 60
			lighting.ClockTime = hours
			wait()
		end
	end
end

The main problem is that I’m not even getting an error so I’m not really sure what I’m doing wrong.

The issue here is that you don’t have any checks for if pause is true inside your time changing loop.

Remember that while loops essentially block off the following chunk of code until desired:
image
This is what is happening with your code.

If you swap the while true do for while pause == false do your code should work as intended.

i’ve made it so that way the loop only runs while not paused

(this is server btw)

local lighting = game:GetService("Lighting")
local minsAfterMidnight = 0
local daylength = 0.1

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage.TimeCycle
pause = false

remoteEvent.OnServerEvent:Connect(function()
	if pause == false then
		pause = true
	else
		pause = false
	end
end)

		while not pause do
			minsAfterMidnight = minsAfterMidnight + 1
			local minutesNormalised = minsAfterMidnight % (60 * 24)
			local hours = minutesNormalised / 60
			lighting.ClockTime = hours
			wait()
		end
local lighting = game:GetService("Lighting")
local minsAfterMidnight = 0
local daylength = 0.1

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage.TimeCycle
local pause = false

remoteEvent.OnServerEvent:Connect(function()
	if pause == false then
		pause = true

        while pause do
			minsAfterMidnight = minsAfterMidnight + 1
			local minutesNormalised = minsAfterMidnight % (60 * 24)
			local hours = minutesNormalised / 60
			lighting.ClockTime = hours
		    wait()
        end
	else
		pause = false
	end
end)

FYI, though the above will fix your issue, this is just a bad design, as it won’t function well on a server with multiple players and is overall just a tedious way to do things.