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

How do I make music play in shuffle?

Asked by 7 years ago

Hey, I'm trying to make like music player with songs that don't go in order, but instead they are shuffled and don't play in order. Here's script;

while true do
    script.Parent.SoundId= "rbxassetid://142304703" --1 Fortunate Son
    script.Parent:Play()    
    wait(120)
    script.Parent.SoundId= "rbxassetid://142304778" --2 I Can See Clearly Now
    script.Parent:Play()    
    wait(120)
    script.Parent.SoundId= "rbxassetid://142304968" --3 Imagine
    script.Parent:Play()    
    wait(120)
    script.Parent.SoundId= "rbxassetid://142305080" --4 Lets Groove
    script.Parent:Play()    
    wait(120)
    script.Parent.SoundId= "rbxassetid://142305170" --5 Mercy Me
    script.Parent:Play()    
    wait(120)

They play in order, but I want them to play randomly in any order. How do I modify it?

1 answer

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

Here's how I would do a song shuffle. Random isn't exactly good enough because you can get the same song twice.

We will construct a table of soundids to use, use a separate table to track what songs we still want to play, remove sound ids based on the known size of the table randomly, and loop.

function clonetable(org)
  return {table.unpack(org)}
end

ids = {"rbxassetid://142304703", "rbxassetid://142304778", "rbxassetid://142304968", "rbxassetid://142305080", "rbxassetid://142305170"}

while true do
   idstouse = clonetable(ids)
   for i = 5,1,-1 do
      script.Parent.SoundId = table.remove(math.random(1,i))
      script.Parent:Play()
      wait(120)
   end
end
0
About the waiting time, you see the 120 wait time in my original script was intended to be like the length of song until it changes to another. Will this shuffle script affect the songs length? Because I'm planning to add songs that are not the same 120 length. ShadowNinja1080 6 — 7y
0
And also your script doesn't work. No songs play. ShadowNinja1080 6 — 7y
0
He gave you a method. In no way is he obligated to give you direct code unmiss 337 — 7y
0
Use Sound:Ended to catch when the song ends http://wiki.roblox.com/index.php?title=API:Class/Sound/Ended User#18718 0 — 7y
Ad

Answer this question