I need help with a script that changes the music between day and night

So I am working on a game that requires this to work. I am trying to make it so the music changes day and night. I have the script but I do not know whether to put it in starterplayerscripts or serverscriptservice. Someone help me please.

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

5 Likes

I’d put it in the scriptserverstorage, because it is mainly used from a server’s perspective.

2 Likes

Thanks for that, but when I tried it the script doesnt work. I think I need a new script.

2 Likes

Do == when using if statements

Revise this - Control Structures | Documentation - Roblox Creator Hub

2 Likes

Put it in ServerScriptService, the same place you put any script you would want running on the server.

Your code also has some syntax errors, you need to use two equals signs for boolean expressions, so they should be:
if game.Lighting.TimeOfDay == "18:00:00" then
and
if game.Lighting.TimeOfDay == "06:00:00" then

And if I’m assuming you are using a separate script to cycle through time of day I would probably recommend using one script for all of this, as well as using :SetMinutesAfterMidnight and tracking the time in a variable in case the time may not fall on exactly 18:00 when the loop runs.

Here is my version:

local gameTime = 0
local timeChangeRate = 0.1 -- how many seconds to wait between each in-game minute

local daytimeMusic = --path to the sound object
local nighttimeMusic = --path to the sound object

local dayStart = 360 -- 6:00 am
local nightStart = 1080 -- 6:00 pm

while (true) do
	gameTime = (gameTime + 1) % 1440
	if (gameTime % dayStart == 0) then
		daytimeMusic:Play()
		nighttimeMusic:Stop()
	elseif (gameTime % nightStart == 0) then
		daytimeMusic:Stop()
		nighttimeMusic:Play()
	end
	game.Lighting:SetMinutesAfterMidnight(gameTime)
	wait(timeChangeRate)
end
4 Likes

Local script? That is what it would be right?

Nevermind it would be a regular script right?

There is an error there, and would the 2 sounds be the children of the script or somewhere else?

You could try something like this(Script in ServerScript, sounds in SoundService, change nightSound and daySound variable directories):

local nightSound = game.SoundService.SoundNameGoesHere -- Enter night sound location here
local daySound = game.SoundService.SoundNameGoesHere --Enter day sound location here

while true do
	local Time = game.Lighting.TimeOfDay
	Time = (Time:gsub("[:\r]", ""))
	Time = tonumber(Time)
	
	if Time >= 180000 or Time < 60000 then
		--Between 1800hrs and 0600hrs
	
		
		wait(0.1)
		if daySound.Playing then daySound:Stop() end
		if not nightSound.Playing then
			nightSound:Play()
		end
	
	else
		--Between 0600 hrs and 1800hrs
		
		if nightSound.Playing then nightSound:Stop() end
		wait(0.1)
		if not daySound.Playing then
			daySound:Play()
		end
	end
end
2 Likes

You need to put the object path to the sound object there. Just copying and pasting pseudocode from the DevForum will not always work. Put the sounds in SoundService or Workspace and the script in ServerScriptService as a server script. It handles both the day/night cycling and the sounds.

I am fairly new to scripting, what is variable directories?

? could you tell me what variable directories are/

I meant that you need to change the nightSound and daySound variables to their respective paths. If your sounds are located in the SoundService, you can simply change SoundNameGoesHere to the name of your sounds.

I did that and still nothing is working

I am still confused can you walk me through it?

Make sure your explorer looks like mine. Both sounds should be in “SoundService”, and the Script should be in “ServerScriptService”. If your explorer looks like mine, you would set the nightSound and daySound variables to the path of both sounds.:

local nightSound = game.SoundService.Sound1 -- Enter night sound location here

local daySound = game.SoundService.Sound2 --Enter day sound location here

If your sounds are called something different, replace “Sound1” and “Sound2” with the names of your sounds. I have just tested this in Studio and it works flawlessly.

I’m afraid that if you still can’t seem to get it to work, I will be unable to assist you any further.


this is what happens and when I load in the sounds dont work

Is there anything else I can try.

You missed a spot at the first line. It’s gotta be game.SoundService.Sound2

I have that look here… Screen Shot 2020-05-05 at 2.34.19 PM