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

Music Playing for Night and Day?

My code only plays Song1

I want it to play Song2 as well for the night time.

script.LocalBackgroundMusic.Parent = game.StarterPlayer.StarterPlayerScripts
game.StarterPlayer.StarterPlayerScripts.LocalBackgroundMusic.Disabled = false
repeat
	if game.Lighting.TimeOfDay == "06:00:00" then
		script.Song1:Resume()
		script.Song2:Stop()
		wait()
	elseif game.Lighting.TimeOfDay == "20:00:00" then
		script.Song1:Stop()
		script.Song2:Resume()
		wait()
	else
		wait()
	end
until script.EqualTrue.Value == false
script:remove()

Also it needs to stay in correspondance to the mute music clip

local enabled = true
script.Parent.Visible = true
function onClick()
	if enabled == false then 
		script.Parent.Parent.Parent.Parent.PlayerScripts.LocalBackgroundMusic.Sound.Volume = 0.5
		enabled = true
		script.Parent.Text = "Music: ON"
	else
		script.Parent.Parent.Parent.Parent.PlayerScripts.LocalBackgroundMusic.Sound.Volume = 0
		enabled = false
		script.Parent.Text = "Music: OFF"
	end
end
script.Parent.MouseButton1Click:connect(onClick)

.

1 answer

✓ Accepted answer

GENERAL PRACTICE

Use :GetService() to get the Lighting and StarterPlayer Service

Use :WaitForChild() to make sure a part exists before you use or change it

An easier way to check the server's time is to use the ClockTime property of Lighting since this is already a number value you can use (from 0 to 24)

You can place the wait() outside of the if-then statement to save space

Activated is preferred over MouseButton1Click since Activated works for all devices and not just computers.

ISSUES

connect is deprecated in favor of Connect

:remove() is deprecated in favor of :Destroy()

In addition, I've added into the code a BoolValue named "MusicBool" under ReplicatedStorage which both scripts can call, regardless of if they are a Server Script or Local Script. When the bool is true, the music will play (the button would say "Music: On")

REVISED SCRIPT #1

local lbm = script:WaitForChild("LocalBackgroundMusic")
lbm.Parent = game:GetService("StarterPlayer"):WaitForChild("StarterPlayerScripts")
lbm.Disabled = false

local light = game:GetService("Lighting")
local song1 = script:WaitForChild("Song1")
local song2 = script:WaitForChild("Song2")
local check = game:GetService("ReplicatedStorage"):WaitForChild("MusicBool")

repeat
	if check.Value == true and light.ClockTime == 6 then
		song1:Resume()
		song2:Stop()
	elseif check.Value == true and light.ClockTime == 20 then
		song1:Stop()
		song2:Resume()
	end
	wait()
until script:WaitForChild("EqualTrue").Value ==  false

script:Destroy()

REVISED SCRIPT #2

local button = script.Parent
button.Visible = true
local sound = button.Parent.Parent.Parent:WaitForChild("PlayerScripts"):WaitForChild("LocalBackgroundMusic"):WaitForChild("Sound")
local check = game:GetService("ReplicatedStorage"):WaitForChild("MusicBool")
local enabled = true

script.Parent.Activated:Connect(function()
	if enabled == false then
		button.Text = "Music: ON"
		sound.Volume = 0.5
		enabled = true
		check.Value = true
	else
		button.Text = "Music: OFF"
		sound.Volume = 0
		enabled = false
		check.Value = false
	end
end)

2 comments

1

Added headings for readability

1

Thank you so much! I am new to coding and I try to learn on my own to code by youtube videos and everytime I seem to get somewhere with coding, the code is depreciated but I already learned it and Roblox takes it still so it makes things difficult for me when it comes to coding. Thanks AGAIN

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