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

Help with math.random?

Asked by
NotSoNorm 777 Moderation Voter
9 years ago

So I have this table with a bunch of song id's and I need help getting it to choose a random ID from the table. Help?

Assets = {161475254,89960960,152669449,145869637,157398365,143836328,155319906,142532442,151666823,150444919,156927530,157428830,154684915,142369626,155623794,156963351,143959455,142720946,142402620,142460434,147680226,153474023,150382165}

zig = script.Parent.MainWall.SurfaceGui.ScrollingFrame.Sound

while true do
    wait(1)
    if zig.IsPlaying == false then
        wait(5)
        if zig.IsPlaying == false then
            zig.SoundId = Assets[math.random(1,100)]
        end
    end
end

2 answers

Log in to vote
1
Answered by
noliCAIKS 210 Moderation Voter
9 years ago

The problem is that you're taking a random number between 1 and 100 while there are only 23 items in the Assets table.

Try changing the this line:

zig.SoundId = Assets[math.random(1,100)]

To this:

zig.SoundId = Assets[math.random(1,#Assets)]

That automatically makes it adjust to the size of the table.

Edit:

Sorry, I kind of messed up there. The actual problems are somewhere else. Unfortunately, the IsPlaying property is not actually set to false when the sound has stopped. Instead, you will manually have to enter the duration of each song in seconds, like this:

Assets = {
    {161475254, 10},
    {89960960, 15},
    {152669449, 9}
}

Note that these are examples. The 10, 15 and 9 are examples of the durations of these sounds. You'll need to look them up for yourself.

The second problem is that you can't just set the SoundId directly to the assetId. Instead you'll have to do this:

zig.SoundId = "rbxassetid://" .. assetId

The third problem is that you don't use zig:Play() anywhere. The sound won't play if you don't tell it to play.

Putting all of these together, the solution is the following:

Songs = {
    {161475254, 10},
    {89960960, 15},
    {152669449, 9} -- Add more songs here. First value on each row is the sound's id, the second value is the duration in seconds. 
}
zig = script.Parent.MainWall.SurfaceGui.ScrollingFrame.Sound

while true do
    local songInfo = Songs[math.random(1, #Songs)] -- Choose a random song.
    zig.SoundId = "rbxassetid://" .. songInfo[1] -- Set the sound id to the song.
    zig:Play() -- Play the sound.
    wait(songInfo[2]) -- Wait for the song to end. You must have set the duration.
    zig:Stop() -- Stop the sound just in case you entered the duration too low.
end
0
That really can't be the problem cause I have 100 song ID's on the actual script, I just thought there were to many to put in there NotSoNorm 777 — 9y
0
@NotSoNorm I edited the answer to include an actual solution. Sorry for overlooking the actual problem. By the way, I tested a few of the soundIds you put in the example, and some of them seemed to be broken, so I recommend you check that. noliCAIKS 210 — 9y
Ad
Log in to vote
-3
Answered by 9 years ago

I'm pretty sure you've got that part right (changing the ID) it's just you haven't played the sound. I haven't done much anything in aaggess so try doing zig:Play() somewhere there xD

0
I did do that on the script its just not working for some reason NotSoNorm 777 — 9y
0
Have you double checked the asset numbers to see if they're correct? infalliblelemon 145 — 9y
0
0 positive NotSoNorm 777 — 9y

Answer this question