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
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!