Sure thing.
For starters, let’s simplify what you’re checking.
Like the monster script, you’re just checking what time it is, right?
Instead of using this:
local Time = game.Lighting.TimeOfDay
Time = (Time:gsub("[:\r]", ""))
Time = tonumber(Time) -- Results in thousands
We can just use clock time again for simpler numbers.
local lighting = game:GetService("Lighting")
local Time = lighting.ClockTime -- Results in 0-24 numbers
Let’s make sure our if statements are simple, and clear. While you can check if a song is playing, it could be hard to read later!
Let’s use another flag like MonstersHaveSpawned
, but we’ll make it a text string called CurrentTime
for understanding. I’m going to set it to None for now.
local CurrentTime = "None" -- Can be Day/Night as well
We use CurrentTime
so that the script will only send the Day/Night instructions once. It’s essentially just like asking “Hey, is it night already? If not, do this!”
if Time >= 6 and Time < 18 and CurrentTime ~= "Day" then -- it isnt day already?
CurrentTime = "Day"
nightSound:Stop()
daySound:Play()
lighting.FogStart = 0
lighting.FogEnd = 100000
daySound:Play()
print("Day time")
-- If it's more or equal then 18 at night
-- And CurrentTime isnt set to night yet, then
elseif Time >= 18 and CurrentTime ~= "Night" then -- its not night already?
CurrentTime = "Night"
daySound:Stop()
nightSound:Play()
lighting.FogStart = 10
lighting.FogEnd = 120
lighting.FogColor = Color3.new(0,0,0)
print("Night time")
end
Combined with our update loop, this is the final script.
-- Get any services you need first to save time
local lighting = game:GetService("Lighting")
-- Use :WaitForChild() on the intro to scripts to prevent any missing errors.
local nightSound = workspace:WaitForChild("nightSound")
local daySound = workspace:WaitForChild("daySound")
local CustomMusic = workspace:WaitForChild("CustomMusic")
--[[
We use a current time variable to prevent the excessive isPlaying checks.
isPlaying is still a good way to check, but this seems simpler given the circumstances.
It's a lot safer to assume things are playing if you told them to before.
--]]
local CurrentTime = "None" -- Feel free to set this to "Day" if you've already played the sounds
--[[ Avoid using wait() when possible!
Wait() is more of a "there is a problem here" then it is for regular code use.
I'd really only use wait() if you needed to time something specific or set how fast something should update
(like this script)
--]]
while task.wait(0.3) do
local Time = lighting.ClockTime
-- Keep your coditional (if then) statements simple, it might hurt your head otherwise!
-- If it's more or equal then 6 in the morning
-- And it's earlier then 18 at night,
-- And CurrentTime isnt set to Day yet, then
if Time >= 6 and Time < 18 and CurrentTime ~= "Day" then
CurrentTime = "Day"
CustomMusic:Play()
nightSound:Stop()
daySound:Play()
lighting.FogStart = 0
lighting.FogEnd = 100000
daySound:Play()
print("Day time")
-- If it's more or equal then 18 at night
-- And CurrentTime isnt set to night yet, then
elseif Time >= 18 and CurrentTime ~= "Night" then
CurrentTime = "Night"
-- CustomMusic:Stop() -- im not sure if you wanted this or not
daySound:Stop()
nightSound:Play()
lighting.FogStart = 10
lighting.FogEnd = 120
lighting.FogColor = Color3.new(0,0,0)
print("Night time")
end
end