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
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)
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
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.