✓ Accepted answer
EDIT:
I thought of a new solution. Instead of using a loop, let's just use the signal that detects whenever the ClockTime is changed.
local sound = workspace.Sound
local sleepsound = workspace.SleepSound
local Lighting = game:GetService("Lighting")
local startOfDay = 6
local startOfNight = 18
script:SetAttribute("Daytime", true)
script:GetAttributeChangedSignal("Daytime"):Connect(function()
local attribute = script:GetAttribute("Daytime")
if attribute == true then
sleepsound:Stop()
sound:Play()
else
sound:Stop()
sleepsound:Play()
end
end)
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
local clockTime = Lighting.ClockTime
if (clockTime >= startOfDay) and (clockTime < startOfNight) then
script:SetAttribute("Daytime", true)
elseif (clockTime >= startOfNight) or (clockTime < startOfDay) then
script:SetAttribute("Daytime", false)
end
end)
OLD:
I recommend use BoolValues or Attributes. Attributes are basically custom "properties" you can make in Instances. When it's day, you make the Attribute true, and false when it's night. Then, when the attribute changes, connect Instance:GetAttributeChangedSignal() signal to a function where it plays a music whether it's day or night.
local sound = game.Workspace:WaitForChild("Sound")
local sleepsound = game.Workspace:WaitForChild("SleepSound")
local timer = game:GetService("Lighting")
script:SetAttribute("Daytime", true)
script:GetAttributeChangedSignal("Daytime"):Connect(function()
local attribute = script:GetAttribute("Daytime")
if attribute == true then
sleepsound:Stop()
sound:Play()
else
sound:Stop()
sleepsound:Play()
end
end)
while task.wait(1) do
if timer.ClockTime >=19 and timer.ClockTime <= 8 then
script:SetAttribute("Daytime", false)
elseif timer.ClockTime <=19 and timer.ClockTime >= 8 then
script:SetAttribute("Daytime", true)
end
end
2 comments
Could you place your code in a code block
Ye