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.
COMMUNITY QUESTION
How do I make random music play?
2 answers
local Ids = {158849566,1238143270,412393959} --id table
local Music = workspace.Sound.Music
local IdChosen
local function RandomId()
local Id = math.random(3) --number of ids in table
IdChosen = Ids[Id]
end
RandomId()
print(IdChosen)
Music.SoundId = "rbxassetid://"..IdChosen
Music:Play()
Music.Ended:Connect(function(soundid)
repeat
RandomId()
until IdChosen ~= soundid -- makes sure no repeat
Music.SoundId = "rbxassetid://"..IdChosen
Music:Play()
end)
this way sould stop it from repeating a song
local overallmusic = game.Workspace.OverallMusic --this is the sound that will be changed/updated
local RunSer = game:GetService("RunService") --checks per frame
local music = {} --Just put a sound id in here, make sure to include "," when adding multiple
RunSer.Heartbeat:Connect(function()
if overallmusic.Playing == false then
local musicpick = music[math.random(1,#music)] --this gets a random song from the music table
overallmusic.SoundId = "rbxassetid://"..musicpick --applies the chosen song
overallmusic.TimePosition = 0 --resets the time
overallmusic:Play()
end
wait()
end)
Hope this helps! :)
1 comment
0
Ok Player1_Joined I love you thank you so much.
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Do you have a list of music for the players to listen to? if you do and they are under a single parent, you can use math.random() to pick a sound and iterate through the parent:GetChildren() table, if the index == random then play the music
I have the SoundId of all the songs and I have math.random() in the script. It plays one random song then just stops.