Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Trying to get a clock to chime every hour, not detecting hour correctly?

Basically, I want a sound to play every hour, however, it keeps looping (it is played when it ends, due to a debounce preventing it spam playing) I use ClockTime in the Lighting space to detect whenever it is dead on an hour, but it doesn't seem to read ClockTime correctly. The code is as follows:

Lighting = game.Lighting
Time = Lighting.ClockTime
Bell = script.Parent.NewtonBells
IsPlaying = false

while true do
  wait()
  if Time == 1 or Time == 2 or Time == 3 or Time == 4 or Time == 5 or Time == 6 or Time == 7 or Time == 8 or Time == 9 or Time == 10 or Time == 11 or Time == 12 or Time == 13 or Time == 14 or Time == 15 or Time == 16 or Time == 17 or Time == 18 or Time == 19 or Time == 20 or Time == 21 or Time == 22 or Time == 23 or Time == 24 then
    if IsPlaying == true then

    else
      Bell:Play()
      IsPlaying = true
      wait(50)
      print("BellFinished")
      Bell:Stop()
      IsPlaying = false
    end
  end
end

15 comments

0

Stop spamming the link in the text chat

0

I haven't spammed anything but ok

0

what's this abomination

0

its an abomination because people are refusing to help...

0

u don't need to do a whole lot of ors just do if Time == Time + 1 then

0

actually no, just make a variable for both the previous value and the new value so local previousvalue = 0 then while true do thing then make sure the new value is = to the new value then check if the new value is equal to the previous value + 1 and then set the previous value to the new value

0

no.

0

yes. if he is trying to play something every hour

0

my problem is that its not actually detecting the ClockTime as the correct value, its constantly thinking the clocktime is everytime, meaning the script repeatedly loops. I get that checking if it was +1 of the previous value, but if the current code doesn't read ClockTime correctly, that won't be of much help

0

New Code: Lighting = game.Lighting Time = Lighting.ClockTime Bell = script.Parent.NewtonBells IsPlaying = false while true do L = game.Lighting.ClockTime wait() if L == 12 or L == 24 then if IsPlaying == false then Bell:Play() print("Bells Ringing") IsPlaying = true wait(50) IsPlaying = false else --Bell Doesn't Ring print("Bell didn't ring ,isRinging") end else p

0

(This new code doesn't seem to detect the time correctly, it doesn't ring at all, and I don't know why)

0

lmao I posted a working code for your problem but I realized that I'm a bad person so I deleted it

0

it wasn't remotely working, but yes you are a bad person, so no one cares anyway

0

Instead of all the "or"s try something like if if Time >= 2 and <= 10 then

0

nope, that wouldn't work.

3 answers

instead of a while loop use runservice heartbeat this is so it can check every frame if it is dead on the hour (meaning it will no matter what)

Also to check if the value is dead on an hour you can just check if value == math.round(value)

ex:

local IsPlaying = false

game:GetService("RunService").Heartbeat:Connect(function()
	local Time = game.Lighting.ClockTime -- I noticed that you only set the value once making this value never on the hour. Make sure it updates in the loop
	if Time == math.round(Time) then
		if not IsPlaying then -- putting "not" before hand will allow the code to run if IsPlaying is either nil or false.
			-- Code to play the sounds
		end
	end
end)

5 comments

0

ah stupid me, I should've remembered variables don't automatically update! I'll try your code, but it does seem it should work anyway!

0

How can I edit the code so I can setup multiple times for it to ring? I presume the math.round(Time) is where I put the time I want it to ring in right?

0

this should be set up for any time thats exactly on the hour, but yes, of course you could set it up for any time

0

and also yes math.Round(Time) is where you would add in other times

0

So it can take multiple values? How many exaclty?

There it should ring every hour in Real Time when it works so next one will be in around 32 Minutes, ask me if there are any errors or questions

Lighting = game.Lighting
Time = Lighting.ClockTime
Bell = script.Parent.NewtonBells
IsPlaying = false

while true do
	wait(0.5)
	local currentMinute = os.date("*t")["min"] -- gets real minute time 
	if currentMinute == 0 then -- will activate in real time when its like 0 minutes so it will ring at 10:00 11:00 12:00 etc.
		if IsPlaying == true then

		else
			Bell:Play()
			IsPlaying = true
			wait(60.5) -- minute passed so it wont repeat
			print("BellFinished")
			Bell:Stop()
			IsPlaying = false
		end
		end
	end

4 comments

0

If you want it to ring at like every 9:30 10:30 and so on put currentMinute == 30

0

If you want it to ring at serverstart tell me you have to get the currentMinute of the server and set it as a variable and then check if currentMinute = starttime

0

I was wanting it to follow the in-game time instead of real-life time. Do you think you could alter the script so that it runs via the game-time please?

0

Macauley i think the issue is that your ingame time is like value = 14 value = 15 but you have to set little intervals like 14.2 so it wont loop

Or you can do

local PreviousValue = 0
local Value = ([[intvalue]]).Value
while true do
    Value = ([[intvalue]]).Value
    if Value == PreviousValue+1 then
        PreviousValue = Value
        -- do stuff
    end
    wait(0.25)

end

Be sure to define lighting and where you put [[intvalue]] is where you put the time's value.

This discussion is preserved as it was. New answers, comments, and votes are closed.