sup defs, i got a problem. I have a script that change music between day or night... everything is okay but every second the playing music start again! How do i fix it?
local sound = game.Workspace:WaitForChild("Sound") local sleepsound = game.Workspace:WaitForChild("SleepSound") local timer = game:GetService("Lighting") while task.wait(1) do if timer.ClockTime >=19 or timer.ClockTime <= 8 then if sound:stop() or sleepsound:play() then sound:Play() sleepsound:stop() end elseif timer.ClockTime <=19 or timer.ClockTime >= 8 then if sleepsound:stop() or sound:play() then sleepsound:Play() sound:Stop() end end end
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) -- creates the attribute script:GetAttributeChangedSignal("Daytime"):Connect(function() local attribute = script:GetAttribute("Daytime") if attribute == true then -- if it's true (day) sleepsound:Stop() sound:Play() else -- if it's false (night) sound:Stop() sleepsound:Play() end end) Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function() local clockTime = Lighting.ClockTime if (clockTime >= startOfDay) and (clockTime < startOfNight) then -- if it's daytime script:SetAttribute("Daytime", true) elseif (clockTime >= startOfNight) or (clockTime < startOfDay) then -- if it's nighttime 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) -- creates the attribute script:GetAttributeChangedSignal("Daytime"):Connect(function() local attribute = script:GetAttribute("Daytime") if attribute == true then -- if it's true (day) sleepsound:Stop() sound:Play() else -- if it's false (night) sound:Stop() sleepsound:Play() end end) while task.wait(1) do if timer.ClockTime >=19 and timer.ClockTime <= 8 then script:SetAttribute("Daytime", false) -- sets the attribute to night elseif timer.ClockTime <=19 and timer.ClockTime >= 8 then script:SetAttribute("Daytime", true) -- sets the attribute to day end end