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