Ad
Log in to vote
1

How to fix my Script that change music between day and night??

Asked by 12 days ago
Edited 12 days ago

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

0
Could you place your code in a code block theking66hayday 501 — 12d
0
Ye yaroslav664 7 — 12d

1 answer

Log in to vote
0
Answered by 12 days ago
Edited 2 days ago

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
0
Sry, but its now working, i'm trying to put ur script in ServerScriptStoragr and i workspace but its doesnt work :( yaroslav664 7 — 10d
0
are you sure it's a normal script? it won't work if it's a local script T3_MasterGamer 1047 — 10d
0
yes yaroslav664 7 — 4d
0
ok, what line does the error occur? T3_MasterGamer 1047 — 4d
View all comments (4 more)
0
if possible, send me a video recording of what's happening T3_MasterGamer 1047 — 4d
0
Just nothing, no errors and no music. Btw we can keep communicate in Discord, there is my username Yarik#3562 yaroslav664 7 — 3d
0
uhh sure T3_MasterGamer 1047 — 2d
0
mine have a user of NotReal T3_MasterGamer 1047 — 2d
Ad

Answer this question