Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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!

4 comments

0

Thank you very much Leamir! Is there any way to repeat the function forever?

1

After line 17 in Leamir's code, just put this: NewSound()

0

Thank you zyrun!

0

Thanks, zyrun!

This discussion is preserved as it was. New answers, comments, and votes are closed.