I recently began working on the background music for my game.
I have created this gorgeous background music play (sarcastic tone. Its actually quite normal)
But the problem is it only plays 1 song then never plays anymore songs?
Heres the code
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SongPlaying = false
local TatooineMusic1 = ReplicatedStorage.AudioStorage:WaitForChild("BackgroundMusicTatooine")
local TatooineMusic2 = ReplicatedStorage.AudioStorage:WaitForChild("BackgroundMusicTatooine2")
while true do
local RandomSong = math.random(1,2)
if RandomSong == 1 and SongPlaying == false then
print(RandomSong)
SongPlaying = true
TatooineMusic1:Play()
TatooineMusic1.Ended:connect(function()
SongPlaying = false
end)
elseif RandomSong == 2 and SongPlaying == false then
print(RandomSong)
SongPlaying = true
TatooineMusic2:Play()
TatooineMusic2.Ended:connect(function()
SongPlaying = false
end)
end
wait()
end
Yes it is a Star Wars game... Dont judge, Its actually pretty good in my opinion.
But anyways anyone have any ideas on how to fix it? Thank you for your time!

3 comments
Thanks, Ive changed my code to yours but it still only plays 1 song then stops. May I add my script is a normal script inside ServerScriptService if that has any effect. But I dont know why it wont play more than 1 song. I think it has to do with the song ending.
you're certain it's all inside the while true do loop? I tried it on my game it seemed to work fine
Yeah I dont know why it wont work