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

MP3 Player Script Will Play The First Song When You Click Next But Not The Second, Any Help?

Asked by
LuaDLL 253 Moderation Voter
7 years ago

So when I click the next button on it it will play the first time but when I click it again it will not play the next song.

Error: Players.FuriaI.PlayerGui.mp3player.BackGround.Main:69: bad argument #2 to 'random' (interval is empty) 09:13:23.975 - Stack Begin 09:13:23.976 - Script 'Players.FuriaI.PlayerGui.mp3player.BackGround.Main', Line 69 - global NextSong 09:13:23.977 - Script 'Players.FuriaI.PlayerGui.mp3player.BackGround.Main', Line 81 Script:

local Player = game.Players.LocalPlayer
repeat wait() until Player.Character
local Character = Player.Character
local Bg = script.Parent
local Bar = Bg.Bar
local PlayPause = Bg.PlayPause
local Prev = Bg.Prev
local Next = Bg.Next
local SongImg = Bg.ImageLabel
local Song = Bg.Song
local Time = Bg.Time
local SongIsPlaying = false

local SoundIds = {
    897623161,844906598,
}

local ChosenSong = {}

local PrevSongg = {}

table.sort(SoundIds)

function GetIndex(tabl)
    for i,v in pairs(tabl) do
        return v
    end
end

function CheckIfPlaying()
    if SongIsPlaying == true then
        return true
    elseif SongIsPlaying == false then
        return false
    end
end

function PlaySong()
    local Sound = Instance.new("Sound")
    Sound.Name = "Music"
    if Character.Torso:FindFirstChild("Music") then
        Character.Torso.Music:Destroy()
    end
    Sound.Parent = Character.Torso
    for i,v in pairs(ChosenSong) do
        Sound.SoundId = "rbxassetid://"..v
    end
    Sound.Looped = true
    Sound.Volume = 1
    Sound.TimePosition = 0
    Sound:Play()
end

function NextSong()
    if CheckIfPlaying() then
        for i,v in pairs(ChosenSong) do
            table.insert(SoundIds,v)
            for g,h in pairs(PrevSong) do
                table.remove(PrevSong,h)
                table.insert(PrevSong,v)
            end
            table.remove(ChosenSong,v)
        end
        local index = math.random(1, #SoundIds);
        table.insert(ChosenSong, SoundIds[index]);
        table.remove(SoundIds, index);
        PlaySong()
    elseif not CheckIfPlaying() then
        local index = math.random(1, #SoundIds);
        table.insert(ChosenSong, SoundIds[index]);
        table.remove(SoundIds, index);
        PlaySong()
    end
end

function PrevSong()
    -- Will Work On This
end

Next.MouseButton1Click:Connect(function()
    NextSong()
end)

Prev.MouseButton1Click:Connect(function()
    PrevSong()
end)

PlayPause.MouseButton1Click:Connect(function()
    if table.getn(ChosenSong) > 0 then
        PlaySong()
    else
        local index = math.random(1, #SoundIds);
        table.insert(ChosenSong, SoundIds[index]);
        table.remove(SoundIds, index);
    end
end)

1 answer

Log in to vote
0
Answered by 7 years ago

This might be my fault, in part, for giving a lackluster answer yesterday :p

When you pick a song, your ChosenSong, you remove the song from the array SoundIds. After a couple of removals, there are no ids left in SoundIds, so #SoundIds returns 0. math.Random(1, 0) errors because (1, 0) is an invalid interval. Think about a jukebox; to play a new song, you have to remove the record on the table and choose a new one.

Try adding the song back into SoundIds when you change songs. Something like:

table.insert(SoundIds, ChosenSong[1]);
ChosenSong = {};
-- table.insert(table, value) inserts value at the end of table, so pick an index between the first and second to last values (don't repeat the song)
local index = math.Random(1, #SoundIds - 1)
table.insert(ChosenSong, SoundIds[index]);
table.remove(SoundIds, index);
0
Thank you man! LuaDLL 253 — 7y
0
You're welcome! :) KidTech101 376 — 7y
Ad

Answer this question