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

Choose a random song to play script help? [SOLVED]

Asked by 6 years ago
Edited 6 years ago

Hello people.

I am currently making a game with a music system, but I recently edited it to try and make it select a random song id from my table using math.random(), but it won't work.

Can someone please help me solve this problem?

Thanks! Here's the script:

local sounds = {

    247020473, -- Tinie Tempah - Pass Out
    404211423, -- The Black Eyed Peas - I Gotta Feeling (Instrumental)
    1062968079, -- Nicki Minaj - Starships
    144567977, -- Olly Murs - Troublemaker (Ft. Flo Rida)
    152102303, -- Owl City - Good Time (Ft. Carly Rae Jepsen)

}
local music = script.Parent

while true do
    wait(5)
    music.SoundId = "rbxassetid://"..math.random(1, #sounds)
    music:play()
    music.Ended:wait(1)

    music.SoundId = "rbxassetid://"..math.random(1, #sounds)
    music:play()
    music.Ended:wait(1)

    music.SoundId = "rbxassetid://"..math.random(1, #sounds)
    music:play()
    music.Ended:wait(1)

    music.SoundId = "rbxassetid://"..math.random(1, #sounds)
    music:play()
    music.Ended:wait(1)

    music.SoundId = "rbxassetid://"..math.random(1, #sounds)
    music:play()
    music.Ended:wait(1)
end

1 answer

Log in to vote
1
Answered by
Leamir 3138 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

Hello, Harrydamastr!

In your script the math.random(1, #sounds) can be 'translated' to math.random(1, 5) so, you have to use SoundSelect = math.random(1, #sounds) music.SoundId = "rbxassetid://" .. sounds[SoundSelect] !

Hope this help!

You use this script:

local sounds = {

    247020473, -- Tinie Tempah - Pass Out
    404211423, -- The Black Eyed Peas - I Gotta Feeling (Instrumental)
    1062968079, -- Nicki Minaj - Starships
    144567977, -- Olly Murs - Troublemaker (Ft. Flo Rida)
    152102303, -- Owl City - Good Time (Ft. Carly Rae Jepsen)

}
local music = script.Parent
local SoundSelect

function NewSound()
    SoundSelect = math.random(1, #sounds)
    music.SoundId = "rbxassetid://" .. sounds[SoundSelect]
    music:play()
    music.Ended:wait(1)
end

NewSound()

Good Luck with your games!

0
Thank you very much Leamir! Is there any way to repeat the function forever? Harrydamastr 35 — 6y
1
After line 17 in Leamir's code, just put this: NewSound() zyrun 168 — 6y
0
Thank you zyrun! Harrydamastr 35 — 6y
0
Thanks, zyrun! Leamir 3138 — 6y
Ad

Answer this question