How to make Clocktime Make Sound?

I want try make Sound of Day/Night on the Clocktime i want like 6 and 20 but idk why my script not work.
Script i put on ServerScriptService and
Sound put on SoundService
also the script have cycle Day/Night:

local Lighting = game:GetService("Lighting")

RunService.Heartbeat:Connect(function()
	Lighting.ClockTime += 0.0005
	task.wait(1)
end)

local L = game:GetService('Lighting')
local morning = game.SoundService.Morning:WaitForChild('Morning')
local night = game.SoundService.Night:WaitForChild('Night')
local playing
while true do
	wait()
	if playing ~= night and (L.ClockTime < 6 or L.ClockTime >= 20) then
		playing = night
		night:Play()
		morning:Pause()
		else if playing ~= morning and L.ClockTime >= 6 and L.ClockTime < 20 then
			playing = morning
			morning:Play()
			night:Pause()
		end
	end
end

I don’t know how to fixed it so can Ya’ll help me?
Thank you.

just a random thought syntax-wise, shouldnt the else if be on the same indentation/level as the original if is? so there would be only two ends? like so:

while true do
	wait()
	if playing ~= night and (L.ClockTime < 6 or L.ClockTime >= 20) then
		playing = night
		night:Play()
		morning:Pause()
	else if playing ~= morning and L.ClockTime >= 6 and L.ClockTime < 20 then
		playing = morning
		morning:Play()
		night:Pause()
	end
end

because if you put the else if in the same level as the if, itll only play if the if is true (if lua would not error it in the first place), so itll only try to play night music? and then once playing changes to night, no ifs will be true anymore?

1 Like

If you wish to mute the sfx’ externally (like a settings menu) you’d have to do it with .Playing = or Stop() and Play() or even PlaybackSpeed = 0 if youre feeling over the top

-- Removed the second lighting variable
-- Added tweens so its not harsh of a transition
-- Mute and unmute through 'volume' rather the Play() and Stop() events
-- Fixed the if statements and removed unnecessary checks
-- Made it a bit cleaner

local Lighting = game:GetService("Lighting")
local SoundService = game:GetService("SoundService")
local TweenService = game:GetService("TweenService")
local tinfo = TweenInfo.new(3)

RunService.Heartbeat:Connect(function()
	Lighting.ClockTime += 0.0005
	task.wait(1)
end)

local morning = SoundService.Morning:WaitForChild('Morning')
local night = SoundService.Night:WaitForChild('Night')

morning.Playing = true
night.Playing = true

while true do
	wait(0.1)
	if Lighting.ClockTime >= 6 and Lighting.ClockTime <= 18 then
		TweenService:Create(night, tinfo, {Volume = 0.5}):Play()
		TweenService:Create(morning, tinfo, {Volume = 0}):Play()
	else
		TweenService:Create(night, tinfo, {Volume = 0}):Play()
		TweenService:Create(morning, tinfo, {Volume = 0.5}):Play()
	end
end

Because you put playing ~= night and playing ~= morning but the variable is nil by default

2 Likes

okay but somehow it still not make sound on 6 or 18, did i dumb or idk?
also script little change cause i ask for help on discord:

local SoundService = game:GetService("SoundService")
local TweenService = game:GetService("TweenService")
local tinfo = TweenInfo.new(3)

task.spawn(function() 
	while task.wait() do
		Lighting.ClockTime = Lighting.ClockTime + 0.0005
	end
end)

local morning = SoundService.Morning:WaitForChild('Morning')
local night = SoundService.Night:WaitForChild('Night')

morning.Playing = true
night.Playing = true

while task.wait() do
	if Lighting.ClockTime >= 6 and Lighting.ClockTime <= 18 then
		TweenService:Create(night, tinfo, {Volume = 0.5}):Play()
		TweenService:Create(morning, tinfo, {Volume = 0}):Play()
	else
		TweenService:Create(night, tinfo, {Volume = 0}):Play()
		TweenService:Create(morning, tinfo, {Volume = 0.5}):Play()
	end
end```

Its an issue with your sounds then

1 Like