Help with music change at night script

i use cinderings background music and i was wondering if at a certain time i could change the sound to night sounds, but im having trouble

This is the script inside the sound parent.

while true do

wait(1)

if game.Lighting.TimeOfDay = "18:00:00" then

script.Parent:Stop()

wait(0.1)

script.Parent.FalloutNVNightAmbient:Play()

elseif game.Lighting.TimeOfDay = "06:00:00" then

script.Parent.FalloutNVNightAmbient:Stop()

wait(0.1)

script.Parent:Play()

end

end

ive tried making it with the SoundId property, but that didn’t work.

3 Likes

Apparently you’re playing the sound over and over again every 1.1 seconds.

An additional problem is that the if statements. They won’t work due to that is not a == operation. It is not recommended to do that, because the signal often skip the time frame(by small fractions). Proceed with ClockTime:

if Lighting.ClockTime >= 6 then
     print("This is an example")
end

Addendum:
The audio, if looped, only needs to be played once, a boolean would fix the problem.

1 Like

but with cinderings background music the sound i want to play at night to stop all other sounds are in a global background music which means anywhere that is not a music zone will play that list of music but what i want to do is when it turns 18:00:00 i want to change all of the sounds in the global music list to that id of the night music. The audio is local so everybody hears different music at the same time.

and at 6 turn it back to the original ids of the music. mostly likely it will have to be a server script. or a local script in StarterPlayerScripts.

you could do something like this:

local lighting = game:GetService("Lighting")
local sound1 = script:WaitForChild("Sound1")
local sound2 = script:WaitForChild("Sound2")
sound1.Looped = true
sound2.Looped = true
sound1:Play()
local function onTimeChanged()
if lighting.ClockTime == 18 then
sound1:Stop()
wait(.1)
sound2:Play()
elseif lighting.ClockTime == 6 then
   sound2:Stop()
wait(.1)
sound1:Play()
   end
end
lighting:GetPropertyChangedSignal("ClockTime"):Connect(onTimeChanged)

**The sounds are children of the script
**You should place the local script in StarterPlayerScripts

3 Likes

An addition of RemoteEvents to signal the audios to change and play should probably be the solution. The lighting check comes from a server script. It’ll send a signal to clients to play the audios.

A collection of SoundId(s) in a table should do the thing with the BGM for transitions. In order to interact with the table: MyTable[index] and for random index, MyTable[math.random(1, #MyTable)] should do the trick.

If you want the sounds to be replicated differently for each client. Use a local script under StarterPlayerScripts.

well as i am only a 15 year old dev i can see what you are trying to say but i dont know how to write that into a script. i dont want to sound rude here just know im not trying to be.

This is what i understand. you want me to make a server script for the lighting but you also want me to make the table of the SoundId(s) in a local script in StarterPlayerScripts and you want me to add remote functions to replicated storage. I understand it but i dont know how to write it in a script. when all global music is in a Folder.

Why aren’t you just making the music system on the client only?
I mean, if it’s only background music, it could be on the client…

1 Like

i didn’t make the music system its cinderings background music toolkit. im just trying to add on to it and make it more alive and have music change at different times. its not needed but its something that would make my game much more alive then having the same music at night and day. im pretty sure it is on the client as everyone hears different music at the same time. i don’t know much about server side scripts and client side. What i do know is im pretty sure scripts are server side and local scripts are client side.

I would recommend reading this: click here

Ostensibly, I have to be direct in this case.

local Lighting = game:GetService("Lighting")

local Day_Sounds = {} -- insert SoundIds here
local Night_Sounds = {} -- insert SoundIds here

local Sound_A -- the main BGM during day sound to change
local Sound_B -- the main BGM during night sound to change

local function onTimeChanged()
    local ID_A = Day_Sounds[math.random(1, #Day_Sounds)]
    local ID_B = Night_Sounds[math.random(1, #Night_Sounds)]

    if Lighting.ClockTime >= 6 then
        -- sound check DAY
        if Sound_B.IsPlaying then
            Sound_B:Stop()
        end
        
        if not Sound_A.IsPlaying then
            Sound_A["SoundId"] = ID_A
            Sound_A:Play()
        end
    elseif Lighting.ClockTime >= 18 or Lighting.ClockTime < 6 then
        -- sound check NIGHT
        if Sound_A.IsPlaying then
            Sound_A:Stop()
        end

        if not Sound_B.IsPlaying then
            Sound_B["SoundId"] = ID_B
            Sound_B:Play()
        end
    end
end

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(onTimeChanged)
3 Likes

hey thanks ill will try to learn something from this just where do i put this?

I guess you could put this in StarterPlayerScripts as a LocalScript. Beware that you need to add two sound instances for the script to work(assigning Sound_A and Sound_B to a Sound Instance.

1 Like

A while ago I created a Plants vs. Zombies Battlegrounds game on here and one of the things I coded was to play day-time music during the day, and vise versa. If I can find the file, I’ll happily open-source it here. It’s FE compatible as well so you don’t have to worry about that.

4 Likes

hey thanks if you can find it ill thank you so much!:grinning:

2 Likes

personally instead of looping i would of used Changed as it fires less but it should not be a issue for you useing while true do