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

This math random music gives error and doesnt play music?

Asked by
TechModel 118
2 years ago
Edited 2 years ago

The local script that is in replicated first plays when the players first joins the game, also show a gui loading screen. I've used math.random but I keep getting errors and I'm not quite getting how to use and where to put math.random. I want the music to be played randomly, and also to keep playing the music once the sound is finished, another one plays.

local GUI = script:WaitForChild("LoadingScreen")
local plr = game.Players.LocalPlayer

GUI.Parent = plr.PlayerGui
script.Parent:RemoveDefaultLoadingScreen()

repeat
    wait(.5)
until game:IsLoaded()

local Sound = script.Sound11
local IDs = {
    1234,1233,1384,29472,94729,2947 (expample)
}

for i = math.random(1, #IDs) do
    local ID = IDs[i]
    Sound.SoundId = "rbxassetid://" .. ID
    Sound:Play()
end

wait(10)

GUI.Frame:TweenPosition(UDim2.new(0,0,-1.5,0),Enum.EasingDirection.InOut,Enum.EasingStyle.Quad, 1)

wait(1)

--for i = 2, 0, -0.01 do
--  Sound.Volume = i
--  print("count")
--  wait(.25)
--end
--Sound:Stop()

GUI:Destroy()

1 answer

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

Here's some code. I'm pretty sure that this code will choose a new song every time.

local soundIds = {1841601203, 1838058421, 1843463175} -- put your soundIds in this table.
local soundObject = workspace.Sound -- your sound object
local lastMusicId = soundIds[1] --don't change this

while true do -- a while loop
    local playingMusicId = soundIds[math.random(1, #soundIds)] --,selects a random song from the soundId table
    print(playingMusicId)

    if playingMusicId == lastMusicId then -- checks if the current chosen music is the same song from last time
        repeat 
                  playingMusicId = soundIds[math.random(1, #soundIds)] wait(1) -- repeats until a the song is not equal to the last on
        until playingMusicId~= lastMusicId

        print(playingMusicId)
        lastMusicId = playingMusicId -- last song will be changed to the current song
    else -- otherwise
        soundObject.SoundId = "rbxassetid://" .. playingMusicId --  plays the song chosen
        soundObject:Play() -- plays it
        soundObject.Ended:Wait() -- waits until it ends and repeats the loop
    end
end

This will run forever since it's a while loop.

0
You can get rid of the prints. I just did it to see if it works properly. By the way this is a  server-script. I think just put it in a local-script so it runs on the client only. MarkedTomato 810 — 2y
0
cheers. apprecaited the help TechModel 118 — 2y
Ad

Answer this question