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!
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
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.
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
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
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)
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 .
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).
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
little tidbit of knowledge