Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 2 years ago
Edited 2 years 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?

01local sound = game.Workspace:WaitForChild("Sound")
02local sleepsound = game.Workspace:WaitForChild("SleepSound")
03local timer = game:GetService("Lighting")
04 
05while task.wait(1) do
06    if timer.ClockTime >=19 or timer.ClockTime <= 8 then
07        if sound:stop() or sleepsound:play() then
08            sound:Play()
09            sleepsound:stop()
10        end
11    elseif timer.ClockTime <=19 or timer.ClockTime >= 8 then
12        if sleepsound:stop() or sound:play() then
13            sleepsound:Play()
14            sound:Stop()
15        end
16    end
17end
0
Could you place your code in a code block theking66hayday 841 — 2y
0
Ye yaroslav664 7 — 2y

1 answer

Log in to vote
2
Answered by 2 years ago
Edited 2 years 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.

01local sound = workspace.Sound
02local sleepsound = workspace.SleepSound
03local Lighting = game:GetService("Lighting")
04 
05local startOfDay = 6
06local startOfNight = 18
07 
08script:SetAttribute("Daytime", true) -- creates the attribute
09script:GetAttributeChangedSignal("Daytime"):Connect(function()
10    local attribute = script:GetAttribute("Daytime")
11    if attribute == true then -- if it's true (day)
12        sleepsound:Stop()
13        sound:Play()
14    else -- if it's false (night)
15        sound:Stop()
View all 28 lines...

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.

01local sound = game.Workspace:WaitForChild("Sound")
02local sleepsound = game.Workspace:WaitForChild("SleepSound")
03local timer = game:GetService("Lighting")
04 
05script:SetAttribute("Daytime", true) -- creates the attribute
06script:GetAttributeChangedSignal("Daytime"):Connect(function()
07    local attribute = script:GetAttribute("Daytime")
08    if attribute == true then -- if it's true (day)
09        sleepsound:Stop()
10        sound:Play()
11    else -- if it's false (night)
12        sound:Stop()
13        sleepsound:Play()
14    end
15end)
View all 23 lines...
0
Sry, but its now working, i'm trying to put ur script in ServerScriptStoragr and i workspace but its doesnt work :( yaroslav664 7 — 2y
0
are you sure it's a normal script? it won't work if it's a local script T3_MasterGamer 2189 — 2y
0
yes yaroslav664 7 — 2y
0
ok, what line does the error occur? T3_MasterGamer 2189 — 2y
View all comments (4 more)
0
if possible, send me a video recording of what's happening T3_MasterGamer 2189 — 2y
0
Just nothing, no errors and no music. Btw we can keep communicate in Discord, there is my username Yarik#3562 yaroslav664 7 — 2y
0
uhh sure T3_MasterGamer 2189 — 2y
0
mine have a user of NotReal T3_MasterGamer 2189 — 2y
Ad

Answer this question