How to make this Clock System work properly?

So, this is a something I can’t figure out on my own.

I have a clock system which is inside script which starts it when the server is up and running.

if game:GetService("RunService"):IsRunning() then
	Start()
end

The Start() triggers this function which is, (Don’t worry on this anyway becz it’s not the issue!)

local function Start()
	Seconds = 0
	Minute = 7
	AM = true
	game.Lighting.ClockTime = 7
	TimeStart = true
	
	coroutine.wrap(function()
		while TimeStart == true do
			Seconds += 1
			game.Lighting.ClockTime += 0.0167 -- Don't touch it or the Day/Night Cycle will get ruined!
			if Seconds == 60 then
				Seconds = 0
				game.Lighting.ClockTime = math.ceil(game.Lighting.ClockTime)
				Minute += 1
				if Minute == 13 then
					Minute = 1
				end
			end
			local realTime = NumberFilter(Minute, Seconds)
			for scheduleTime, classInfo in pairs(SchoolScheduleDict) do
				if realTime == tostring(scheduleTime) then
					for _, person in pairs(game:GetService("Players"):GetChildren()) do
						if person.Team == game.Teams.Teen then -- If the person is TEEN!
							for i,BunkStudent in pairs(SchoolBunkers) do
								if person == BunkStudent then -- If the person didn't bunk the school!
									break
								end
								if i == #SchoolBunkers then
									game:GetService("ReplicatedStorage").RemoteFunctions.SchoolPrompt:InvokeClient(person, classInfo[2])
									if LatestClass ~= classInfo[1] then
										LatestClass = classInfo[1]
									end
								end 
							end
						end
					end
				end
			end
			if realTime == "01:00AM" then
				table.clear(SchoolBunkers)
				table.insert(SchoolBunkers, "A")
				print("REFRESH TIME")
			end
			game:GetService("ReplicatedStorage").RemoteEvents.GlobalTime:FireAllClients(realTime)
			wait(1)
		end
	end)()
end

So, When I play inside the studio it works perfectly even when I publish the game and shutdown all servers it plays the Clock System perfectly but after a while some servers doesn’t even load the Start() function. It looks like the Start() function didn’t even triggered in the first place which is something I can’t get over my head. Tried different methods and every single one is doing the same thing.

(If you want to investigate the issue more!)

This is the full script, It's inside ServerScriptService
local TimeStart = false
local Seconds = 0
local Minute = 0
local AM = false

--First Class "School Starts" (08:00AM)
--Second Class "Math Class" (09:00AM)
--Third Class "Science Class" (10:00AM)
--Forth Class "English Class" (12:00AM)
--Fith Class "Lunch" (1:00PM)
--Six Class "PE Class" (2:00PM)
--Fina Class "Go Home" (3:00PM)

local SchoolBunkers = {"A"}

local SchoolScheduleDict = {
	["08:00AM"] = {workspace.School.TPtoCentreOfSchool, "School is open!"},
	["09:00AM"] = {workspace.School.MathClass, "Math Class!"},
	["10:00AM"] = {workspace.School.ScienceClass, "Science Class!"},
	["12:00AM"] = {workspace.School.EnglishClass, "English Class!"},
	["01:00PM"] = {workspace.School.Lunch, "Lunch Break!"},
	["02:00PM"] = {workspace.School.PE, "PE Class!"},
	["03:00PM"] = {workspace.School.GoHome, "School is done for today!"}
}

local function NumberFilter(Minute, Seconds)
	local fullTime
	local Hours = Minute
	local Min = Seconds
	if Minute < 10 then
		Hours = tostring("0"..Minute)
	end
	if Seconds < 10 then
		Min = tostring("0"..Seconds)
	end

	if Minute == 12 and Seconds == 1  then
		if AM == true then
			AM = false
		else
			AM = true
		end
	end

	fullTime = tostring(Hours..":"..Min.."AM")
	if AM == false then
		fullTime = tostring(Hours..":"..Min.."PM")
	end

	return fullTime
end

local function BunkDictionary(client, doInsert)
	if doInsert == true then
		table.insert(SchoolBunkers, client)
		print("PLAYER BUNKED!")
	else
		table.clear(SchoolBunkers)
		table.insert(SchoolBunkers, "A")
		print("REFRESH TIME")
	end
end

local LatestClass
local function TPrequest(client)
	if client.Character then
		client.Character.Humanoid.Jump = true
		repeat wait() until client.Character.Humanoid.Jump == false
		client.Character.HumanoidRootPart.CFrame = LatestClass.CFrame
	end
end

local function Start()
	Seconds = 0
	Minute = 7
	AM = true
	game.Lighting.ClockTime = 7
	TimeStart = true
	
	coroutine.wrap(function()
		while TimeStart == true do
			Seconds += 1
			game.Lighting.ClockTime += 0.0167 -- Don't touch it or the Day/Night Cycle will get ruined!
			if Seconds == 60 then
				Seconds = 0
				game.Lighting.ClockTime = math.ceil(game.Lighting.ClockTime)
				Minute += 1
				if Minute == 13 then
					Minute = 1
				end
			end
			local realTime = NumberFilter(Minute, Seconds)
			for scheduleTime, classInfo in pairs(SchoolScheduleDict) do
				if realTime == tostring(scheduleTime) then
					for _, person in pairs(game:GetService("Players"):GetChildren()) do
						if person.Team == game.Teams.Teen then -- If the person is TEEN!
							for i,BunkStudent in pairs(SchoolBunkers) do
								if person == BunkStudent then -- If the person didn't bunk the school!
									break
								end
								if i == #SchoolBunkers then
									game:GetService("ReplicatedStorage").RemoteFunctions.SchoolPrompt:InvokeClient(person, classInfo[2])
									if LatestClass ~= classInfo[1] then
										LatestClass = classInfo[1]
									end
								end 
							end
						end
					end
				end
			end
			if realTime == "01:00AM" then
				table.clear(SchoolBunkers)
				table.insert(SchoolBunkers, "A")
				print("REFRESH TIME")
			end
			game:GetService("ReplicatedStorage").RemoteEvents.GlobalTime:FireAllClients(realTime)
			wait(1)
		end
	end)()
end

game:GetService("ReplicatedStorage").RemoteFunctions.SchoolPrompt.OnServerInvoke = function(player, said) 
	if said == true then -- accpet
		TPrequest(player)
	elseif said == false then -- bunk
		BunkDictionary(player, true)
	end
end

if game:GetService("RunService"):IsRunning() then
	Start()
end

IsRunning() should return false if youre in studio every time

A more appropriate name for IsRunning would probably be IsActualServer but that doesnt flow off the tip of the tongue quite as nicely

It would lead me to believe that theres a problem with another part of your script if its running in studio when the start function shouldnt even be getting called because IsRunning() in studio is always false

Have you tried putting a print statement at the very top, inside the Start() function to see if its being ran?

1 Like

Yes, IsRunning() works and it fires one time when I start the game. I already tested it with the “print” method to check if this fires 2wice in a row or not and it seems fine. Will update this if something works or not!

Will go for the default put nothing in the end line and just go for the trigger anyways,
Instead of going to wait for something to fire It should fire when the script loads… so It would work to be honest, For efficiency’s sake I didn’t wanted to do this but think it would work.

1 Like