Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[SOLVED] My music player script plays two songs at once. Fix?

Asked by 5 years ago
Edited 5 years ago

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!

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Try this:

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
    wait(s..n.TimeLength)
    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)

So that wait(s..n.TimeLength) will let the infinite loop wait for the song to finish before choosing another song randomly.

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Actually I've figured it out. Since a script is 'restarted' when it's disabled and then re-enabled, I can make a script just for playing the music and make another script for changing the button's text and enabling and disabling the music player script.

Disabler/Enabler Script

--Main variables--
local btn = script.Parent
local db = true --debounce
local val = script.Parent.Toggle

--Music variables--
local ss = game.SoundService
local s1 = ss.Sound1
local s2 = ss.Sound2
local s3 = ss.Sound3
local s4 = ss.Sound4
local s5 = ss.Sound5

btn.MouseButton1Click:Connect(function()
    if db == true then
        db = false
        if script.Parent.MusicPlayer.Disabled == false then
            script.Parent.MusicPlayer.Disabled = true
            btn.Text = "Music OFF"
            s1.Playing = false --Each song would need to be toggled off
            s2.Playing = false --or else they would keep playing while
            s3.Playing = false --the script (when it resets) plays
            s4.Playing = false --another song.
            s5.Playing = false
            wait(0.2)
            db = true
        elseif script.Parent.MusicPlayer.Disabled == true then
            script.Parent.MusicPlayer.Disabled = false
            btn.Text = "Music ON"
            wait(0.2)
            db = true
        end
    end
end)

Music Player Script

local ss = game.SoundService
local s1 = ss.Sound1
local s2 = ss.Sound2
local s3 = ss.Sound3
local s4 = ss.Sound4
local s5 = ss.Sound5

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

Answer this question