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

How do I make random music play?

Asked by 4 years ago

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.

0
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 SerpentineKing 3885 — 4y
0
I have the SoundId of all the songs and I have math.random() in the script. It plays one random song then just stops. therealninetailz 0 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
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

Ad
Log in to vote
0
Answered by 4 years ago
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! :)

0
Ok Player1_Joined I love you thank you so much. therealninetailz 0 — 4y

Answer this question