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.
It interests me on where you can see that.
I will check it tomorrow though if it works. Don’t have much time today left.
Hmm, I think I know now on how I could fix it, but can’t test it today anymore.
We replace that first with the other version, where each number represents a ID.
But since I haven’t got time to test it, aka can’t access my laptop, could someone else confirm it for me?
What exactly are you trying to do with the loops that change the volume? They yield the script for exorbitant amounts of time to just change volume.
Anyway, I removed the loops and moved the math.randomseed to be at the beginning of the script instead of inside the while true do loop and it appears to be randomizing properly.
Don’t really know, I grabbed the script from a open-sourced game and tried to fix on what i could understand. I don’t really have much of an idea what the volume could be for.
Your original random picker would work. The issue was with the code not running multiple times. The only issue with the original is that it might pick the same id multiple times in a row. To fix that you could just check that the current picked song isn’t the same as the last one.
Okay, well, do you want me to rewrite the volume so it fades in better, and also make sure it doesn’t pick the same song twice?
Those loops are meant to tween the audios volume so it fades in/out. Unless you have id’s that start and stop obtrusively, you probably don’t need those inner loops. If you do though, I recommend using something like tween service to handle it instead.
I used print debugging to find out where the script was stopping.
Here is a rewritten version. It works in my studio.
I added a system of generating a randomized playlist to ensure no song is repeated until after all other songs have played.
math.randomseed(tick())
local Themes = {733298638, 2533140135, 2381174213, 721477744, 705342826, 706837670, 516104812, 705334495, 705326459, 2441582257, 1571892231}
local MaxVolume = .25
-- Amount of seconds to wait before playing the next song.
local DelayTime = 1
-- Create a deep copy of the theme song list.
local songQueue = {}
for _, id in ipairs(Themes) do
table.insert(songQueue, id)
end
-- Have a function ready to shuffle the music list.
function randomizeTable(a, b)
return (math.random() < 0.5)
end
-- I modified the while-loop condition to stop it from crashing if no songs are present.
while (#songQueue > 0) do
print("shuffling playlist...")
table.sort(songQueue, randomizeTable)
-- Go through each track in order.
for track, song in ipairs(songQueue) do
print("playing song: "..track)
print("song id: "..song)
script.Parent.SoundId = "rbxassetid://"..song
script.Parent:Play()
script.Parent.Ended:Wait()
wait(DelayTime)
end
print("end of playlist reached.")
end
[random edit]: This is an old post and I would now recommend not to use the method I had for randomizing tables. It has a chance of erroring due what table.sort expects out of the comparator function. Use this method instead for shuffling tables:
Well, looks like someone beat me to the punch, but this is what I made.
local Themes = {733298638, 2533140135, 2381174213, 721477744, 705342826, 706837670, 516104812, 705334495, 705326459, 2441582257, 1571892231}
math.randomseed(tick())
local lastSong = nil
local MaxVolume = .25
local ts = game:GetService'TweenService'
while true do
ts:Create(script.Parent, TweenInfo(5), {Volume = 0}):Play()
local chosenSong = Themes[math.random(1,#Themes)]
while chosenSong == lastSong do
chosenSong = Themes[math.random(1,#Themes)]
end
print("Chosen song ID is: " .. chosenSong)
script.Parent.SoundId = "rbxassetid://" .. chosenSong
ts:Create(script.Parent, TweenInfo(5), {Volume = MaxVolume}):Play()
script.Parent:Play()
script.Parent.Ended:Wait()
lastSong = chosenSong
end
Edit: I need to stop working while sleep deprived, forgot a few things.
Thanks for all your help. I will test both tomorrow, since today was an absolute trainwreck and I need a break.
Aka sleep, good night
I’ve tested both and none of them played a sound.
The code from @Blokav just played no sound. Even when it successfully printed out everything that it should.
The code from @MrMauio had some errors:
It also played no sound as Blokav’s code did.
Are they even linked to that?
My code worked when I tested it with a freshly inserted sound instance. Could you show us what the properties of the Music object look like prior to the game running?
If the play message is printing but there’s no sound the volume might just be too low.
Well, that won’t work since i can’t test it with Roblox Studio, only what it looks like in Studio right now.
Set “Looped” to false and make sure “Volume” is at .25
Also you can test games in studio with these buttons:
It works perfectly fine now! Thank you very much! ^^
Well, It’s just that the game breaks by doing that, thats why i had to upload it to roblox all the time and test it there then.
Also, which message should I check in as the answer?
That’s alright, I used to have that problem too on a different computer.
Your answer should be whichever message you think solved the problem you were having.