Hello fellow developers, currently I’m trying to make my music switch, but when it changes back from game music to lobby music both still play at the same time?
please help me I cannot understand why it only works for one switch
local SoundService = game.SoundService
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local lobbyMusic = {
["Romantical"] = 1,
["Romania"] = 2,
["RomanLegions"] = 3,
}
local gameMusic = {
["Sword Fight"] = 1
}
local playingMusic = {}
local function getSortedMusicKeys(musicTable)
local keys = {}
for name, _ in pairs(musicTable) do
table.insert(keys, name)
end
table.sort(keys, function(a, b)
return musicTable[a] < musicTable[b]
end)
return keys
end
local function getCurrentState()
local character = player.Character or player.CharacterAdded:Wait()
local parent = character and character.Parent
if parent then
return parent.Name
end
return ""
end
local function playMusicLoop(musicKeys, expectedState)
local index = 1
while getCurrentState() == expectedState do
local songName = musicKeys[index]
local song = SoundService:FindFirstChild(songName)
if song then
print("Now playing:", songName)
song:Play()
table.insert(playingMusic, song)
while song.IsPlaying do
if getCurrentState() ~= expectedState then
song:Stop()
return
end
wait(0.1)
end
else
warn("Song not found:", songName)
wait(1)
end
index += 1
if index > #musicKeys then
index = 1
end
end
end
while true do
local state = getCurrentState()
if state == "Lobby" then
playMusicLoop(getSortedMusicKeys(lobbyMusic), "Lobby")
elseif state == "Game" then
playMusicLoop(getSortedMusicKeys(gameMusic), "Game")
end
wait(0.1)
end
its only when it switches back from the game is when it still plays.
The amount of loops here seem counter-productive. What are you trying to achieve exactly?
Also, I don’t see how the function getCurrentState() outputs “Lobby” or “Game”.
No erros unluckily the only problem is when switching back to lobby after a game the game music still plays? But going from lobby to game there are no issues
local SoundService = game.SoundService
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local lobbyMusic = {
["Romantical"] = 1,
["Romania"] = 2,
["RomanLegions"] = 3,
}
local gameMusic = {
["Sword Fight"] = 1
}
local playingMusic = {}
local function getSortedMusicKeys(musicTable)
local keys = {}
for name, _ in pairs(musicTable) do
table.insert(keys, name)
end
table.sort(keys, function(a, b)
return musicTable[a] < musicTable[b]
end)
return keys
end
local function getCurrentState()
local character = player.Character or player.CharacterAdded:Wait()
local parent = character and character.Parent
if parent then
return parent.Name
end
return ""
end
local function playMusicLoop(musicKeys, expectedState)
local index = 1
while true do
if getCurrentState() ~= expectedState then
for _, song in ipairs(playingMusic) do
song:Stop()
end
playingMusic = {}
break
end
local songName = musicKeys[index]
local song = SoundService:FindFirstChild(songName)
if song then
song:Play()
table.insert(playingMusic, song)
while song.IsPlaying and getCurrentState() == expectedState do
task.wait(0.1)
end
end
index = index + 1
if index > #musicKeys then
index = 1
end
task.wait(0.1)
end
end
while true do
local state = getCurrentState()
if state == "Lobby" then
playMusicLoop(getSortedMusicKeys(lobbyMusic), "Lobby")
elseif state == "Game" then
playMusicLoop(getSortedMusicKeys(gameMusic), "Game")
end
task.wait(0.1)
end
I fixed it the best I could if this doesn’t work then idk what to do I’m a newer to scripting on Roblox
local SoundService = game.SoundService
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local lobbyMusic = {
["Romantical"] = 1,
["Romania"] = 2,
["RomanLegions"] = 3,
}
local gameMusic = {
["Sword Fight"] = 1
}
local playingMusic = {}
local function getSortedMusicKeys(musicTable)
local keys = {}
for name, _ in pairs(musicTable) do
table.insert(keys, name)
end
table.sort(keys, function(a, b)
return musicTable[a] < musicTable[b]
end)
return keys
end
local function getCurrentState()
local character = player.Character or player.CharacterAdded:Wait()
local parent = character and character.Parent
if parent then
return parent.Name
end
return ""
end
local function stopAllPlayingMusic()
for _, song in ipairs(playingMusic) do
song:Stop()
end
playingMusic = {}
end
local function playMusicLoop(musicKeys, expectedState)
local index = 1
stopAllPlayingMusic()
while getCurrentState() == expectedState do
local songName = musicKeys[index]
local song = SoundService:FindFirstChild(songName)
if song then
song:Play()
table.insert(playingMusic, song)
while song.IsPlaying do
if getCurrentState() ~= expectedState then
song:Stop()
return
end
task.wait(0.1)
end
else
task.wait(1)
end
index = index + 1
if index > #musicKeys then
index = 1
end
end
end
while true do
local state = getCurrentState()
if state == "Lobby" then
playMusicLoop(getSortedMusicKeys(lobbyMusic), "Lobby")
elseif state == "Game" then
playMusicLoop(getSortedMusicKeys(gameMusic), "Game")
end
task.wait(0.1)
end
Thanks for the help but I just fixed it! I added a stopAllMusic() function that stops all Sound objects currently under SoundService, regardless of whether they were tracked in the playingMusic table. This ensures a complete cleanup of any active music before new music starts.
local SoundService = game.SoundService
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local lobbyMusic = {
["Romantical"] = 1,
["Romania"] = 2,
["RomanLegions"] = 3,
}
local gameMusic = {
["Sword Fight"] = 1
}
local playingMusic = {}
local function getSortedMusicKeys(musicTable)
local keys = {}
for name, _ in pairs(musicTable) do
table.insert(keys, name)
end
table.sort(keys, function(a, b)
return musicTable[a] < musicTable[b]
end)
return keys
end
local function getCurrentState()
local character = player.Character or player.CharacterAdded:Wait()
local parent = character and character.Parent
if parent then
return parent.Name
end
return ""
end
local function stopAllMusic()
for _, sound in ipairs(SoundService:GetChildren()) do
if sound:IsA("Sound") then
sound:Stop()
end
end
playingMusic = {}
end
local function playMusicLoop(musicKeys, expectedState)
local index = 1
stopAllMusic()
while true do
if getCurrentState() ~= expectedState then
stopAllMusic()
break
end
local songName = musicKeys[index]
local song = SoundService:FindFirstChild(songName)
if song then
song:Play()
table.insert(playingMusic, song)
while song.IsPlaying and getCurrentState() == expectedState do
task.wait(0.1)
end
end
index = index + 1
if index > #musicKeys then
index = 1
end
task.wait(0.1)
end
end
while true do
local state = getCurrentState()
if state == "Lobby" then
playMusicLoop(getSortedMusicKeys(lobbyMusic), "Lobby")
elseif state == "Game" then
playMusicLoop(getSortedMusicKeys(gameMusic), "Game")
end
task.wait(0.1)
end