Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

2 comments

0

Could you place your code in a code block

0

Ye

1 answer

✓ 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) -- 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

8 comments

0

Sry, but its now working, i'm trying to put ur script in ServerScriptStoragr and i workspace but its doesnt work :(

0

are you sure it's a normal script? it won't work if it's a local script

0

yes

0

ok, what line does the error occur?

0

if possible, send me a video recording of what's happening

0

Just nothing, no errors and no music. Btw we can keep communicate in Discord, there is my username Yarik#3562

0

uhh sure

0

mine have a user of NotReal

This discussion is preserved as it was. New answers, comments, and votes are closed.