I dont want the song to be the same one right after it plays once in a row. how do i fix?
local songValues = {} local songs = script.Parent:GetChildren() for i,v in pairs(songs) do if v:IsA("IntValue") and v.Name == "MusicId" then table.insert(songValues, v.Value) end end local sound = Instance.new("Sound",script.Parent) sound.Name = "MusicPlayer" sound.Pitch = pitch sound.Volume = volume while true do sound.SoundId = "rbxassetid://"..songValues[math.random(1,#songValues)] sound:Play() wait(songPlayTime) sound:Stop() wait(nextSongPlayTime) end
First approach
Well first, I recommend you create a pseudo table you can use to control what random values you can access, and eliminate the already chosen ones.
Second approach
Next, I'll just be optimizing some minor details that aren't really relevant in this situation, but it certainly doesn't hurt. I'll go over most of it in commented text.
-- Creating a reference to the script's parent (because why not) local SongHolder = script.Parent -- The array that will contain all the song Ids local Songs = {} -- Get the IntValues from the script's parent and assort it appropriately to the array. Here I'm using a numeric for loop, just because I find it easier to identify with when iterating an array. for i = 1,#SongHolder:GetChildren() do local v = SongHolder[i] if v:IsA("IntValue") and v.Name == "MusicId" then Songs[i] = v.Value -- Insert the value to the Songs list end end
Now for the part you're looking for, the random
selection loop. Here we'll use a while
loop, that will continuously run a numeric for
loop with different values to choose from each time. Here's an example:
local SoundPlayer local SongHolder = script.Parent local Songs = {} for i = 1,#SongHolder:GetChildren() do local v = SongHolder[i] if v:IsA("IntValue") and v.Name == "MusicId" then Songs[i] = v.Value end end -- Create the sound instance SoundPlayer = Instance.new("Sound",SongHolder) -- Main loop while true do -- The new table we'll use to manipulate random values from local selection = {} -- Basically clone the "Songs" table to the "selection" table for i = 1,#Songs do selection[i] = Songs[i] end -- Now comes the main for loop that will do the actually selecting of the random songs for i = 1,#selection do -- Acquire our random index local rand = math.random(#selection) -- Remove the value from what we can choose from again, while setting the SoundPlayer's sound Id to the value (since table.remove returns the value you're removing) SoundPlayer.SoundId = "rbxassetid://"..tostring(table.remove(selection,rand)) -- Play the sound SoundPlayer:Play() -- Don't move to the next random song until the current one has finished playing. SoundPlayer.Ended:wait() -- End the main for loop end -- End the main while loop end
Summary
So basically all we're doing is creating another table that holds the same references as our initial "Songs" table, and removing the random values we choose as we go down the numeric for loop, to prevent choosing the same random value again.
Questions
Hope this helped, let me know if you have any questions.