How can I play different music in the lobby and during the round?

Here is script

-- Arrays of asset IDs for lobby and round music
local lobby_id = {"rbxassetid://1844683265"}
local round_id = {"rbxassetid://1842725760"}

-- Music sound objects in the workspace
local lobby_music = workspace.Music["Lobby music"]
local round_music = workspace.Music["Round music"]

-- IsGame BoolValue to determine the current game state
local IsGame = game.ReplicatedStorage:WaitForChild("IsGame")

-- Function to handle music changes based on game state
IsGame:GetPropertyChangedSignal("Value"):Connect(function()
	if IsGame.Value then
		-- When the game starts, stop lobby music and play round music
		lobby_music:Stop()
		local chosen_round_music = round_id[math.random(1, #round_id)]
		round_music.SoundId = chosen_round_music
		round_music:Play()
	else
		-- When the game ends, stop round music and play lobby music
		round_music:Stop()
		local chosen_lobby_music = lobby_id[math.random(1, #lobby_id)]
		lobby_music.SoundId = chosen_lobby_music
		lobby_music:Play()
	end
end)

-- Start with lobby music by default
local chosen_lobby_music = lobby_id[math.random(1, #lobby_id)]
lobby_music.SoundId = chosen_lobby_music
lobby_music:Play()

What should I add?

Can you be more specific about your question? Just by reading your code I can’t find the problem that you want to solve.

1 Like

My musics doesn’t work, should I make parts?

Your code works just fine. Here’s a place file with it working. I made no changes to the code.

Sound.rbxl (54.9 KB)

Thank you so much! It’s works.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.