Clock Chime sound triggered by :GetMinutesAfterMidnight()

Ight so I got a bit of a problem.
I would like this script to work but It only works once then stops.

So the script will check if its Midnight, 6am, Noon and 6pm. And if it is those times. At each time the clock chime should play. It does but only once. (For example, open a new baseplate and just put this script into a “WallClock” model. By default the baseplate time is 14:00 so the clock will chime at 18:00 (18) then stop. But when midnight rolls around the chime wont play.
however I have the time set at 05:00 (5) so the clock only chimes at 0600 (6) but doesn’t for Noon. Idk what the problem is.

I did at one point have the if game… statements reading with a >=, but that made it chime endlessly regardless of the time. Any Ideas because I don’t know what to do. If there is something other than a While wait(0.05) do Loop, please let me know (and give an example because my coding skills are a solid 3/10) lol Thank you so much xD

Script:

local chimeSpot = game.Workspace.TownClock.WallClock.HandCenter.Sound

while wait(0.05) do
	
	if game.Lighting:GetMinutesAfterMidnight() == 0.1 * 60 then
		chimeSpot:Play()
		print("Play Midnight")
		wait(24)
		chimeSpot.IsPlaying = false
		chimeSpot:Stop()
		print("Stop Midnight")
	end
	
	
	if game.Lighting:GetMinutesAfterMidnight() == 6 * 60 then
		chimeSpot:Play()
		print("Play 6am")
		wait(24)
		chimeSpot.IsPlaying = false
		chimeSpot:Stop()
		print("Stop 6am")
	end
	
	if game.Lighting:GetMinutesAfterMidnight() == 12 * 60 then
		chimeSpot:Play()
		print("Play Noon")
		wait(24)
		chimeSpot.IsPlaying = false
		chimeSpot:Stop()
		print("Stop Noon")
	end
	
	if game.Lighting:GetMinutesAfterMidnight() == 18 * 60 then
		chimeSpot:Play()
		print("Play 6pm")
		wait(24)
		chimeSpot.IsPlaying = false
		chimeSpot:Stop()
		print("Stop 6pm")
	end
end

Yes you can use another way other than a loop. Do Lighting:GetPropertyChangedSignal(“ClockTime”) and run the checks for what time it is. If clocktime == 0 or clocktime == 6 or clocktime == 12 or clocktime == 18 then do what you want. (I removed lol since I didn’t want to seem rude)

-- Chad method (no im no chad)
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
      if clocktime == 0 then 
      elseif clocktime == 6 then 
      elseif clocktime == 12 then
      elseif clocktime == 18 then
      end
end)
-- I don't use clock scripts that often so apologies if my script is innaccurate and bugs out

Oh don’t worry you weren’t rude xD, I was actually about to ask how the Lighting:GetProperty etc. etc. was worded because my mind made the code break xD But you already gave an example so thank you! :slight_smile:

1 Like

Is this the correct setup?
because It doesn’t work (not to sound rude either xD)
its telling me that “Lighting” and “clocktime” are unknown. I presume I need to assign variables like Local Lighting = game.Lighting etc. But I just wanted to run it through you first :slight_smile:

Lighting:GetPropertyChangedSignal("ClockTime"):connect(function()
	if clocktime == 0 then
		chimeSpot:Play()
	elseif clocktime == 6 then
		chimeSpot:Play()
	elseif clocktime == 12 then
		chimeSpot:Play()
	elseif clocktime == 18 then
		chimeSpot:Play()
	end
end)

Oh okay yeah clocktime and Lighting need to become variables but instead of setting clocktime to a variable do Lighting.ClockTime in the script since its not really a good idea to set variables to part properties.

local chimeSpot = game.Workspace.TownClock.WallClock.HandCenter.Sound
local Lighting = game.Lighting
local clocktime = Lighting.ClockTime

code from my previous post after variables

yes?

Maybe in my script replace clocktime in the property changed signal function to Lighting.Clocktime since there isn’t a need for a clocktime variable. But otherwise you’re fine. Yeah don’t use clocktime variable it’s never a good idea to set a variable to the property of something.

Lighting:GetPropertyChangedSignal("ClockTime"):connect(function()
	if Lighting.ClockTime == 0 then
		chimeSpot:Play()
	elseif Lighting.ClockTime == 6 then
		chimeSpot:Play()
	elseif Lighting.ClockTime == 12 then
		chimeSpot:Play()
	elseif Lighting.ClockTime == 18 then
		chimeSpot:Play()
	end
end)

Hmm. Odd, I still can’t get it to work. I have it the same as you; and my locals are only chimeSpot and Lighting.

Code just in case you wanna see:

local chimeSpot = game.Workspace.TownClock.WallClock.HandCenter.Sound
local Lighting = game.Lighting

Lighting:GetPropertyChangedSignal("ClockTime"):connect(function()
	if Lighting.ClockTime == 0 then
		chimeSpot:Play()
	elseif Lighting.ClockTime == 6 then
		chimeSpot:Play()
	elseif Lighting.ClockTime == 12 then
		chimeSpot:Play()
	elseif Lighting.ClockTime == 18 then
		chimeSpot:Play()
	end
end)

edit: I ever tried

local chimeSpot = script.Parent.WallClock.HandCenter.Sound

just in case the game.workspace wasn’t correct; still not working :frowning:

Will print statements print? Also, make the c in Connect uppercase.

‘c’ was changed to ‘C’ and no print statements don’t show. I have one before and after chimeSpot:Play() in all clocktime statements. If i made sense. :slight_smile:

Oh okay, I’ll try to look for solutions.

Take your time, I really appreciate your help. Even if no solution is made xD

Don’t compare numbers with == unless they’re always integers. ClockTime is not!

Your initial instinct to use >= was correct, but you need to keep track of whether or not you’ve already played a chime during the current block of time.

Also, use LightingChanged event instead of a while loop to make it cleaner :slight_smile:

For example (add your stuff to the Chime function):

local times = { 0, 6, 12, 18 }

-- which time we're in or after currently
local function GetCurrentTimeIndex()
	local idx = 1
	for i = 1, #times do
		if game.Lighting.ClockTime >= times[i] then
			idx = i
		end
	end
	return idx
end

local currentTimeIndex = GetCurrentTimeIndex() -- get our starting time block

local function Chime()
	-- do whatever you want in here (e.g. play a sound)
	print(string.format("CHIME!!! It's %d o'clock!", game.Lighting.ClockTime))
end

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	local newIndex = GetCurrentTimeIndex() -- check what time block we're in now

	if newIndex ~= currentTimeIndex then
		Chime()
	end

	currentTimeIndex = newIndex
end)
2 Likes

Try @nicemike40 's solution. It’s very optimized.

It doesn’t work though, just tested it :slight_smile: one sec…

Fixed!

Just so that I can understand, the GetCurrentTimeIndex checks if the time is passed one of the times in the times table and returns the time block that it’s in such as past midnight or 6 am or 12 pm or 6 pm, and then checks if the current time block has been passed and chimes and then sets the time block that is to be passed again to new index. Correct? This solution is pretty optimized lol .

Actually just edited it slightly to be simpler :slight_smile:

But yes, more or less!

GetCurrentTime() function figures out which time block you’re in. For example, say it was 10 o’clock. It goes:

  • Is 10 >= 0? Yes, so idx = 0
  • Is 10 >= 6? Yes, so idx = 1
  • Is 10 >= 12? No (do nothing, idx stays at 1).
  • Is 10 >= 18? No.
  • Return 1 (representing the 6-o’clock chunk).

Then, whenever the time of day changes, we see if we’re in a different time chunk than we were before (if newIndex ~= currentTimeIndex).

If we changed times (such as going from 23:59 to 00:00), we Chime()!

Also just for the future if you do want to compare numbers directly you can do something like

if math.abs(myNumber - myTarget) < 0.001 then
  -- they're close
end

instead of

if myNumber == myTarget then
  -- they're exactly equal (which only works for integers)
end

(this would still not give you a good solution for this particular problem because if the time jumped from e.g. 11:59 PM to 12:01 AM it wouldn’t chime).

Sorry for my late response but you’ve got it :smiley:

Thank you! And though it wasn’t directly directed towards me, thank you for explaining how the idx system is and how the code checks those time chunks :smiley:
little tidbit of knowledge :smiley:

Yes yes, I did :smiley: I also just wanted to thank you as well! I appreciate it :smiley:

1 Like