Stopping time changing script

How can I stop this script from changing the time when needed.

game:GetService("ContentProvider"):Preload("rbxassetid://255808299") --Day
game:GetService("ContentProvider"):Preload("rbxassetid://251613697") --Night
game:GetService("ContentProvider"):Preload("rbxassetid://173499922") --6:00 AM

if script.Parent ~= game.Workspace then
	script.Parent = game.Workspace
end

game.Workspace.ChildAdded:connect(function(c)
	local player = game.Players:GetPlayerFromCharacter(c)
	if player then
		local gui = player:FindFirstChild("PlayerGui")
		for i=1,60 do
			if gui then
				break
			else
				gui = player:FindFirstChild("PlayerGui")
			end
			wait()
		end
		if gui then
			local o = script.TimeHeader:Clone()
			o.Parent = gui
			print("TimeHeader Given To Player '" .. player.Name .. "'")
		end
	end
end)

for _,v in pairs(game.Workspace:GetChildren()) do
	local player = game.Players:GetPlayerFromCharacter(v)
	if player then
		local gui = player:FindFirstChild("PlayerGui")
		for i=1,60 do
			if gui then
				break
			else
				gui = player:FindFirstChild("PlayerGui")
			end
			wait()
		end
		if gui then
			local th = gui:FindFirstChild("TimeHeader")
			if not th then
				local o = script.TimeHeader:Clone()
				o.Parent = gui
				print("TimeHeader Given To Player '" .. player.Name .. "'")
			end
		end
	end
end

local DaySound = Instance.new("Sound",script)
DaySound.Name = "Day Ambient"
DaySound.SoundId = "rbxassetid://255808299"
DaySound.Looped = true
DaySound.Volume = 0

local AMSound = Instance.new("Sound",script)
AMSound.Name = "6 AM"
AMSound.SoundId = "rbxassetid://173499922"
AMSound.Looped = false
AMSound.Volume = 0.6

local NightSound = Instance.new("Sound",script)
NightSound.Name = "Night Ambient"
NightSound.SoundId = "rbxassetid://251613697"
NightSound.Looped = true
NightSound.Volume = 1

DaySound:Play()
NightSound:Play()

local secs = 0

local mins = 0

local hours = 0

local h = script:WaitForChild("Hours")
local m = script:WaitForChild("Minutes")
local s = script:WaitForChild("Seconds")
local ampm = script:WaitForChild("AMPM")

local transitionDay = false
local transitionNight = false

while true do
	
	secs = secs + 1
	
	if secs >= 60 then
		mins = mins + 1
		secs = 0
	end
	
	if mins >= 60 then
		hours = hours + 1
		mins = 0
	end
	
	if hours >= 24 then
		hours = 0
	end
	
	if hours == 6 and mins == 0 and secs == 0 then
		AMSound:Play()
	end
	
	if hours == 6 or transitionDay then
		transitionDay = true
		DaySound.Volume = DaySound.Volume + 0.0025
		NightSound.Volume = NightSound.Volume - 0.0025
		if NightSound.Volume <= 0 then
			transitionDay = false
		end
	end
	
	if hours == 18 or transitionNight then
		transitionNight = true
		DaySound.Volume = DaySound.Volume - 0.0025
		NightSound.Volume = NightSound.Volume + 0.0025
		if DaySound.Volume <= 0 then
			transitionNight = false
		end
	end
	
	game.Lighting.TimeOfDay = hours..":"..mins..":"..secs
	
	if hours >= 12 then
		if hours == 12 then
			h.Value = hours
			ampm.Value = "PM"
		else
			h.Value = hours - 12
		end
	else
		if hours == 0 then
			h.Value = 12
			ampm.Value = "AM"
		else
			h.Value = hours
		end
	end
	m.Value = mins
	s.Value = secs
	
	wait()
	
end

Unrelated to your question, however you can do ``player:WaitForChild(“PlayerGui”), this will make sure the UI has loaded in.

It’s a rather long script you got, which part more precisely is it you want to stop?

1 Like

I want that the part where the script actually changes the time of day to stop when I need it to. It’s the final part. The first part is only the GUI that displays the time.

local secs = 0

local mins = 0

local hours = 0

local h = script:WaitForChild("Hours")
local m = script:WaitForChild("Minutes")
local s = script:WaitForChild("Seconds")
local ampm = script:WaitForChild("AMPM")

local transitionDay = false
local transitionNight = false

while true do
	
	secs = secs + 1
	
	if secs >= 60 then
		mins = mins + 1
		secs = 0
	end
	
	if mins >= 60 then
		hours = hours + 1
		mins = 0
	end
	
	if hours >= 24 then
		hours = 0
	end
	
	if hours == 6 and mins == 0 and secs == 0 then
		AMSound:Play()
	end
	
	if hours == 6 or transitionDay then
		transitionDay = true
		DaySound.Volume = DaySound.Volume + 0.0025
		NightSound.Volume = NightSound.Volume - 0.0025
		if NightSound.Volume <= 0 then
			transitionDay = false
		end
	end
	
	if hours == 18 or transitionNight then
		transitionNight = true
		DaySound.Volume = DaySound.Volume - 0.0025
		NightSound.Volume = NightSound.Volume + 0.0025
		if DaySound.Volume <= 0 then
			transitionNight = false
		end
	end
	
	game.Lighting.TimeOfDay = hours..":"..mins..":"..secs
	
	if hours >= 12 then
		if hours == 12 then
			h.Value = hours
			ampm.Value = "PM"
		else
			h.Value = hours - 12
		end
	else
		if hours == 0 then
			h.Value = 12
			ampm.Value = "AM"
		else
			h.Value = hours
		end
	end
	m.Value = mins
	s.Value = secs
	
	wait()
	
end

You can wrap all of that code inside an if-statement:

while true do
    if timeShouldChange then
        ... -- Your time changing code
    end
    task.wait() -- Task.wait should be used instead of wait.
    -- Note: You don't need to call it this often, I would recommend
        -- waiting around 0.2 -> 1.0 seconds since you only care about
        -- whole seconds and not milliseconds.
end

Then, simply change the value of timeShouldChange (or whatever you wish to call it) to false in order to pause it.

I’m not that good with scripting but can you change it so I could use a NumberValue.
Like

0 = Script is not paused
1 = Script is paused

I’m not sure why you want numbers, but its possible.

Replace my previous if statement with the following:

if timeShouldChange == 0 then

If you don’t know how if-statements work I’d recommend reading this:
Conditional Structures (roblox.com)

I used your original idea, but I have no clue why it isn’t working.

game:GetService("ContentProvider"):Preload("rbxassetid://255808299") --Day
game:GetService("ContentProvider"):Preload("rbxassetid://251613697") --Night
game:GetService("ContentProvider"):Preload("rbxassetid://173499922") --6:00 AM

if script.Parent ~= game.Workspace then
	script.Parent = game.Workspace
end

game.Workspace.ChildAdded:connect(function(c)
	local player = game.Players:GetPlayerFromCharacter(c)
	if player then
		local gui = player:FindFirstChild("PlayerGui")
		for i=1,60 do
			if gui then
				break
			else
				gui = player:FindFirstChild("PlayerGui")
			end
			wait()
		end
		if gui then
			local o = script.TimeHeader:Clone()
			o.Parent = gui
			print("TimeHeader Given To Player '" .. player.Name .. "'")
		end
	end
end)

for _,v in pairs(game.Workspace:GetChildren()) do
	local player = game.Players:GetPlayerFromCharacter(v)
	if player then
		local gui = player:FindFirstChild("PlayerGui")
		for i=1,60 do
			if gui then
				break
			else
				gui = player:FindFirstChild("PlayerGui")
			end
			wait()
		end
		if gui then
			local th = gui:FindFirstChild("TimeHeader")
			if not th then
				local o = script.TimeHeader:Clone()
				o.Parent = gui
				print("TimeHeader Given To Player '" .. player.Name .. "'")
			end
		end
	end
end

local DaySound = Instance.new("Sound",script)
DaySound.Name = "Day Ambient"
DaySound.SoundId = "rbxassetid://255808299"
DaySound.Looped = true
DaySound.Volume = 0

local AMSound = Instance.new("Sound",script)
AMSound.Name = "6 AM"
AMSound.SoundId = "rbxassetid://173499922"
AMSound.Looped = false
AMSound.Volume = 0.6

local NightSound = Instance.new("Sound",script)
NightSound.Name = "Night Ambient"
NightSound.SoundId = "rbxassetid://251613697"
NightSound.Looped = true
NightSound.Volume = 1

DaySound:Play()
NightSound:Play()

local secs = 0

local mins = 0

local hours = 0

local h = script:WaitForChild("Hours")
local m = script:WaitForChild("Minutes")
local s = script:WaitForChild("Seconds")
local ampm = script:WaitForChild("AMPM")
local clock = script:WaitForChild("Time")

local transitionDay = false
local transitionNight = false

while true do
	if clock then
		
		secs = secs + 1

		if secs >= 60 then
			mins = mins + 1
			secs = 0
		end

		if mins >= 60 then
			hours = hours + 1
			mins = 0
		end

		if hours >= 24 then
			hours = 0
		end

		if hours == 6 and mins == 0 and secs == 0 then
			AMSound:Play()
		end

		if hours == 6 or transitionDay then
			transitionDay = true
			DaySound.Volume = DaySound.Volume + 0.0025
			NightSound.Volume = NightSound.Volume - 0.0025
			if NightSound.Volume <= 0 then
				transitionDay = false
			end
		end

		if hours == 18 or transitionNight then
			transitionNight = true
			DaySound.Volume = DaySound.Volume - 0.0025
			NightSound.Volume = NightSound.Volume + 0.0025
			if DaySound.Volume <= 0 then
				transitionNight = false
			end
		end

		game.Lighting.TimeOfDay = hours..":"..mins..":"..secs

		if hours >= 12 then
			if hours == 12 then
				h.Value = hours
				ampm.Value = "PM"
			else
				h.Value = hours - 12
			end
		else
			if hours == 0 then
				h.Value = 12
				ampm.Value = "AM"
			else
				h.Value = hours
			end
		end
		m.Value = mins
		s.Value = secs

		wait()

	end

	end
task.wait()
	

You assign Clock to a time object. Your if-statement checks whether clock exists or not. Is there anything within your Time object which changes depending on if you want it to change or not?