I'm trying to make random music play in my game. Not a specific order for the music to play just random. I've tried so many different ways and none work I am confusion.
01 | local Ids = { 158849566 , 1238143270 , 412393959 } --id table |
02 |
03 | local Music = workspace.Sound.Music |
04 |
05 | local IdChosen |
06 | local function RandomId() |
07 | local Id = math.random( 3 ) --number of ids in table |
08 | IdChosen = Ids [ Id ] |
09 | end |
10 |
11 | RandomId() |
12 |
13 | print (IdChosen) |
14 |
15 | Music.SoundId = "rbxassetid://" ..IdChosen |
this way sould stop it from repeating a song
01 | local overallmusic = game.Workspace.OverallMusic --this is the sound that will be changed/updated |
02 | local RunSer = game:GetService( "RunService" ) --checks per frame |
03 | local music = { } --Just put a sound id in here, make sure to include "," when adding multiple |
04 | RunSer.Heartbeat:Connect( function () |
05 | if overallmusic.Playing = = false then |
06 | local musicpick = music [ math.random( 1 ,#music) ] --this gets a random song from the music table |
07 | overallmusic.SoundId = "rbxassetid://" ..musicpick --applies the chosen song |
08 | overallmusic.TimePosition = 0 --resets the time |
09 | overallmusic:Play() |
10 | end |
11 | wait() |
12 | end ) |
Hope this helps! :)