I want to improve my music player script. Right now, when a player presses a button, the music is toggle on/off. However, since the script waits for the sound to end, if the sound is paused, the script doesn't exit the "if" statement, meaning that if that sound ends after being toggled back on, it will play two sounds. (sorry if that's confusing, basically my script sometimes plays two songs at once)
local btn = script.Parent
local db = true
local val = script.Parent.Toggle
local ss = game.SoundService
local s1 = ss.Sound1
local s2 = ss.Sound2
local s3 = ss.Sound3
local s4 = ss.Sound4
local s5 = ss.Sound5
val.Changed:Connect(function()
if val.Value == true then
btn.Text = "Music ON"
while true do
local n = math.random(1,5)
if n == 1 then
s1:Play()
wait(0.1)
db = true
s1.Ended:Wait()
elseif n == 2 then
s2:Play()
wait(0.1)
db = true
s2.Ended:Wait()
elseif n == 3 then
s3:Play()
wait(0.1)
db = true
s3.Ended:Wait()
elseif n == 4 then
s4:Play()
wait(0.1)
db = true
s4.Ended:Wait()
elseif n == 5 then
s5:Play()
wait(0.1)
db = true
s5.Ended:Wait()
end
end
elseif val.Value == false then
btn.Text = "Music OFF"
s1.Playing = false
s2.Playing = false
s3.Playing = false
s4.Playing = false
s5.Playing = false
end
end)
Please help if you know a solution, thanks!
