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

How do i make this script make a random selection but (different) random selection?

Asked by 8 years ago

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
0
Please edit your question and include the code in a Code Block. It will make understanding the code easier. The Code Block button is above the text field when editing the question, next to the Bold and Italics buttons (it looks like the Lua logo). TheArmoredReaper 173 — 8y
0
Once the code is arranged within a Code Block, I will be able to help you, because I already have the answer, but I do not know how your script actually looks like (is it a single line? Or 4? Maybe 73? I do not know!) TheArmoredReaper 173 — 8y
0
there you go gracefulsuperman 5 — 8y

1 answer

Log in to vote
2
Answered by 8 years ago

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 whileloop, 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.

0
okay, i tried what you said, but i might need a dumbed down version if you dont mind, it doesnt seem to be working for me. i have Musicid's grouped with an objectvalue, then i have the script in the songs value. am i missing something? gracefulsuperman 5 — 8y
0
@ScriptGuider, on like 30, Can we really just do math.random(#selection)? I always do it like this selection[math.random(1,#selection)] buoyantair 123 — 8y
0
Yeah. Having a second argument is optional, which is just for getting a random number within a specific range. For example, if you wanted a random number between 50 and 100, you'd say math.random(50,100). But if you just wanted a random number between 1 and 100, you could just say math.random(100). ScriptGuider 5640 — 8y
Ad

Answer this question