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:
01 | Lighting = game.Lighting |
02 | Time = Lighting.ClockTime |
03 | Bell = script.Parent.NewtonBells |
04 | IsPlaying = false |
05 |
06 | while true do |
07 | wait() |
08 | 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 |
09 | if IsPlaying = = true then |
10 |
11 | else |
12 | Bell:Play() |
13 | IsPlaying = true |
14 | wait( 50 ) |
15 | print ( "BellFinished" ) |
16 | Bell:Stop() |
17 | IsPlaying = false |
18 | end |
19 | end |
20 | end |
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:
01 | local IsPlaying = false |
02 |
03 | game:GetService( "RunService" ).Heartbeat:Connect( function () |
04 | 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 |
05 | if Time = = math.round(Time) then |
06 | if not IsPlaying then -- putting "not" before hand will allow the code to run if IsPlaying is either nil or false. |
07 | -- Code to play the sounds |
08 | end |
09 | end |
10 | end ) |
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
01 | Lighting = game.Lighting |
02 | Time = Lighting.ClockTime |
03 | Bell = script.Parent.NewtonBells |
04 | IsPlaying = false |
05 |
06 | while true do |
07 | wait( 0.5 ) |
08 | local currentMinute = os.date( "*t" ) [ "min" ] -- gets real minute time |
09 | 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. |
10 | if IsPlaying = = true then |
11 |
12 | else |
13 | Bell:Play() |
14 | IsPlaying = true |
15 | wait( 60.5 ) -- minute passed so it wont repeat |
Or you can do
01 | local PreviousValue = 0 |
02 | local Value = ( [[intvalue]] ).Value |
03 | while true do |
04 | Value = ( [[intvalue]] ).Value |
05 | if Value = = PreviousValue+ 1 then |
06 | PreviousValue = Value |
07 | -- do stuff |
08 | end |
09 | wait( 0.25 ) |
10 |
11 | end |
Be sure to define lighting and where you put [[intvalue]] is where you put the time's value.