Audio repeating itself instead of using another Audio

So, I’m trying to have a permanent audio player that switches to another soundid after the currently played one finished, but the thing is, it doesn’t switch, it just repeats the last played soundid.

The Script is placed inside Music, which is in Workspace
Music - Roblox Studio 03.02.2020 19_34_37
The Script looks like this:

local Themes = {733298638, 2533140135, 475011760, 721477744, 705342826, 706837670, 516104812, 705334495, 705326459, 2441582257, 1571892231}

local MaxVolume = .25

while true do

    while script.Parent.Volume > 0 do
        script.Parent.Volume = script.Parent.Volume - wait()/40
    end

    script.Parent.SoundId = "rbxassetid://" .. Themes[math.random(#Themes)]
    script.Parent.TimePosition = 0

    while script.Parent.Volume < MaxVolume do
        script.Parent.Volume = math.min(MaxVolume,script.Parent.Volume + wait()/40)
    end

    wait(math.random(360,600))
end

The most I tried was changing this part:

Themes[math.random(#Themes)]

I also think that said part is the main issue, but I can’t think of any way to actually randomize the table of Id’s

I would be really thankful if you could help me figure this out!

1 Like

Forgive me if my solution doesn’t work, but I do remember reading somewhere that having number values only in tables make their key their value?? It’s a bit complicated, but can you try to enclose every value in your Themes table in quotation marks? Like

local Themes = {"733298638", "2533140135", "475011760", "721477744", "705342826", "706837670", "516104812", "705334495", "705326459", "2441582257", "1571892231"}

Report back to me if it works or not!

Welp, that didn’t change anything. The problem is still there.

I think using quotation marks or not doesn’t even make a difference.

The table isn’t the problem, that works perfectly fine. Just one of the randomizers aren’t working as they should.

But thanks for trying!

Real quick, could you try switching script.Parent.SoundId = "rbxassetid://" .. Themes[math.random(#Themes)] to

math.randomseed(tick()) 
local chosenSong = Themes[math.random(1,table.getn(Themes))]
print("Chosen song ID is: " .. chosenSong)
script.Parent.SoundId = "rbxassetid://" .. chosenSong

Tell me if it prints out random songs and not the same value!

It doesn’t print out anything when the audio ended and played again.

Basically the ID won’t be printed again when that happens.

Is your Music object looped? I think I may know whats happening…

Well, with that problem, I would say yes.

Are you waiting the whole 6-10 minutes it would take to switch audio in your testing? Or better yet changing those values for testing purposes?

wait(math.random(360,600))

I would have it wait(script.Parent.TimeLength) then add a small random value after testing if you want a random delay between ids

Oof, well…
It actually would be better if the audios switch after a audio stops running. If the problem would’ve been just me with my inpatience, then ;-;

In that case instead of using the wait(math.random(360,600)),
you could use script.Parent.Ended:Wait()
It will yield the script until the sound has stopped playing.

I would use what @Blokav suggested for the delay in your loop
For randomization, what you are doing is fine, though I might add a check to make sure it doesn’t play the same audio 2 times in a row. If you wanted it to play the whole list before repeats though I would copy the ID list and remove each picked element until the copied list is empty, then repeat.

After that and waiting for a bit longer, it still didn’t work yet.

Your line has a typo:

Themes[math.random(#Themes)]

Change it to this:

Themes[math.random(1, #Themes)]

Try changing the wait line to this:

script.Parent:Play()
script.Parent.Ended:Wait()
script.Parent:Stop()

When you call math.random with one argument the lower bound defaults to 1.

2 Likes

Still doesn’t work… ;-; Same results again

What does your updated code look like? From what I can tell it should be working.

local Themes = {733298638, 2533140135, 2381174213, 721477744, 705342826, 706837670, 516104812, 705334495, 705326459, 2441582257, 1571892231}

local MaxVolume = .25

while true do

    while script.Parent.Volume > 0 do
	    script.Parent.Volume = script.Parent.Volume - wait()/40
    end

    math.randomseed(tick()) 
    local chosenSong = Themes[math.random(1,table.getn(Themes))]
    print("Chosen song ID is: " .. chosenSong)
    script.Parent.SoundId = "rbxassetid://" .. chosenSong

    while script.Parent.Volume < MaxVolume do
	    script.Parent.Volume = math.min(MaxVolume,script.Parent.Volume + wait()/40)
    end

    script.Parent:Play()
    script.Parent.Ended:Wait()
    script.Parent:Stop()
end

A looped audio doesn’t fire .Ended. Just disable looping and you should be good to go. Though I will note that math.randomseed won’t really do you any good here. The randomness isn’t going to be noticeably effected by changing that every time

I ran the code in studio. Those loops that change the volume are causing the script to hang. It’s just not progressing past that while script.Parent.Volume > 0 do section.

Also do what @tlr22 said and make sure the Looped property is left un-checked.