Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make sounds play at a specific time?

Asked by 3 years ago
Edited 3 years ago

How do I make sounds play at a specific time? I am trying to make this phone play these sounds when it turns midnight. I'm not getting any errors, but the sounds aren't playing. Like, at all. And I am also trying to get them to play at different days of the week, but I don't know if it's the day tracker on here that's messing everything up or not.

local Night = 1

while true do
    wait(0.5)
    if game.Lighting.ClockTime == 0 then
        if Night == 1 then
            script.Parent.Night1:play()
            print(Night)
            Night + 1
        end
        if Night == 2 then
            script.Parent.Night2:play()
            print(Night)
            Night + 1
        end
        if Night == 3 then
            script.Parent.Night3:play()
            print(Night)
            Night + 1
        end
        if Night == 4 then
            script.Parent.Night4:play()
            print(Night)
            Night + 1
        end
        if Night == 5 then
            script.Parent.Night5:play()
            print(Night)
            Night + 1
        end
        if Night == 6 then
            print(Night)
            Night + 1
        end
        if Night == 7 then
            print(Night)
            Night - 6
        end
    else
        wait()
    end
end

And here is the code for the script that changes the time.

local dayLength = 24 --How many minutes a full day(24 hours) will last

local cycleTime = dayLength*60
local minutesInADay = 24*60

local lighting = game:GetService("Lighting")

local startTime = tick() - (lighting:GetMinutesAfterMidnight()/ minutesInADay)*cycleTime
local endTime = startTime + cycleTime

local timeRatio = minutesInADay / cycleTime

if dayLength == 0 then
    dayLength = 1
end

repeat
    local currentTime = tick()

    if currentTime > endTime then
        startTime = endTime
        endTime = startTime + cycleTime
    end

    lighting:SetMinutesAfterMidnight((currentTime - startTime)*timeRatio)
    wait(1/15)
until false
0
Can you show me how you are incrementing your ClockTime? MarkedTomato 810 — 3y
0
Yes TheCrazy_Adventures 0 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

How are you changing the time in your game?

Swap out the while loop with a :GetPropertyChangedSignal("ClockTime"). What I see here is that all 5 sounds play all at once if that's what you want otherwise, you can add something like this

script.Parent.Night1.Ended:Wait()

Always avoid using wait(n), the best solution is using :Wait() with events.

When changing the time or doing something like this, always do it on the Server via Script.

local Lighting = game:GetService("Lighting")

local Night = 1

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    if Lighting.ClockTime == 0 then
        if Night == 1 then
            script.Parent.Night1:play()
            script.Parent.Night1.Ended:Wait()
            print(Night)
            Night + 1
        end
        if Night == 2 then
            script.Parent.Night2:play()
            script.Parent.Night2.Ended:Wait()
            print(Night)
            Night + 1
        end
        if Night == 3 then
            script.Parent.Night3:play()
            script.Parent.Night3.Ended:Wait()
            print(Night)
            Night + 1
        end
        if Night == 4 then
            script.Parent.Night4:play()
            script.Parent.Night4.Ended:Wait()
            print(Night)
            Night + 1
        end
        if Night == 5 then
            script.Parent.Night5:play()
            script.Parent.Night5.Ended:Wait()
            print(Night)
            Night + 1
        end
        if Night == 6 then
            print(Night)
            Night + 1
        end
        if Night == 7 then
            print(Night)
            Night - 6
        end
    end
end
0
It didn't work. Like, at all. I though there was a typo and so I went ahead and put 'print("SampleText")' in various points to see if that was the problem. The "print" didn't even show. I though maybe I should try it on a different scrip. I created a new one, disabled the old one, edited the default "print" to something more recognizable. That worked. So I went ahead and pasted your code into it- TheCrazy_Adventures 0 — 3y
0
and I left that "print" since that's a peace of code that I know works. Nothing happened. That "print" statement didn't even show. I deleted the code and it appeared again in the Output feed. TheCrazy_Adventures 0 — 3y
Ad

Answer this question