The plugin im currently coding plays music when it is launched, it is supposed to play a different song each time it is launched, however when I launch the plugin, it plays the same song each time, it doesn’t change at all
(id’s are cut out for development reasons)
local PLAYLIST = {"rbxassetid://##########","rbxassetid://##########","rbxassetid://##########","rbxassetid://##########","rbxassetid://##########"}
local SONG = PLAYLIST[math.random(1,#PLAYLIST)]
local SoundService = game:GetService("SoundService")
local function playLocalSound(soundId)
-- Create a sound
local sound = Instance.new("Sound")
sound.SoundId = soundId
sound.Volume = 10
-- Play the sound locally
SoundService:PlayLocalSound(sound)
sound.Ended:Wait()
sound:Destroy()
end
local function onNewScriptButtonClicked()
script.Parent.Parent = game.StarterGui
script.Parent.Enabled = true
playLocalSound(SONG)
end
I have tried out math.randomseed and they do the same thing, in which it plays the same music
Please ignore the error, the id’s have since been fixed

This is using the original code
Here are some attempts I made to fix the problem
math.randomseed(os.time())
local SONG = PLAYLIST[math.random(1, #PLAYLIST)]
math.randomseed(tick()%1*1e6)
This bug has been stopping me from making progress so any advice is appreciated!
You don’t generate pick a new song when you click on the button, you just play the same song you selected at the start of the script. To fix this, I would do:
local function onNewScriptButtonClicked()
script.Parent.Parent = game.StarterGui
script.Parent.Enabled = true
local SONG = PLAYLIST[math.random(1, #Playlist)]
playLocalSound(SONG)
end
2 Likes
you’d have to create new random numbers every time it’s played. You’re currently reusing the random chosen id over and over again.
1 Like
local PLAYLIST = {“rbxassetid://##########”,“rbxassetid://##########”,“rbxassetid://##########”,“rbxassetid://##########”,“rbxassetid://##########”}
local SoundService = game:GetService(“SoundService”)
local function playLocalSound(soundId)
– Create a sound
local sound = Instance.new(“Sound”)
sound.SoundId = soundId
sound.Volume = 10
– Play the sound locally
SoundService:PlayLocalSound(sound)
sound.Ended:Wait()
sound:Destroy()
end
local function onNewScriptButtonClicked()
local SONG = PLAYLIST[math.random(1,#PLAYLIST)]
script.Parent.Parent = game.StarterGui
script.Parent.Enabled = true
playLocalSound(SONG)
end
This will work.
1 Like
Thank you so much! This works perfectly!
although as a side note is it possible to loop the audio and when you close the plugin it stops the music? Anyways thanks for the solution!
The question is, what plugin? If you tell me what plugin that is and yes, Its possible.
The plugin im making is for development purposes regarding patching scripts with new code, music is just for the fun of it lol, if its possible how is it achieved?
1 Like
sound:Pause()
sound:Resume()
Correct me if I’m mistaken but is it this that you’re talking about?
1 Like
About, closing the plugin?
I’m quite confused
1 Like
Sorry! When I mean closing the plugin it closes the GUI and stops the music, however I want it to destroy the music as opposed to pausing it, although looping im not sure how to do
so any help is appreciated 
1 Like
At the moment I do not have a way to loop and stop the music since they are all contained in one function
local function playLocalSound(soundId)
-- Create a sound
local sound = Instance.new("Sound")
sound.SoundId = soundId
sound.Volume = 10
-- Play the sound locally
SoundService:PlayLocalSound(sound)
sound.Ended:Wait()
sound:Destroy()
end
And I want to be able to stop the music, but I can not use the local as its contained within the function
And because of that this is my current closing script…
local function EXIT()
script.Parent.Enabled = false
end
script.Parent.EXIT.MouseButton1Click:Connect(EXIT)
its very basic and I have not yet found a way to close it properly and opening it without erroring
1 Like
local getSound
local function playLocalSound(soundId)
-- Create a sound
local sound = Instance.new("Sound")
sound.SoundId = soundId
sound.Volume = 10
getSound = sound
-- Play the sound locally
SoundService:PlayLocalSound(sound)
sound.Ended:Wait()
if getSound then
game.Debris:AddItem(getSound, 0)
end
end
local function EXIT()
script.Parent.Enabled = false
if getSound then
if pcall(function() getSound:Destroy() end) then
print("Sound has been completely destroyed.")
end
end
end
script.Parent.EXIT.MouseButton1Click:Connect(EXIT)
Wanna give this an examination? It might work probably.
Hey! Sorry for the late reply, I went to sleep, anyways the code works somewhat, the looping doesnt work and when I close the plugin it also doesnt stop the music, however when I close the plugin it prints out saying it destroyed the music, but I could still hear it.
1 Like
What do you mean by looping? as infinite loop.
Yes as infinite loop, when the song ends, it plays it again, but stops once you close the plugin
1 Like
I did a little bit of edit, wanna give it a try?
sure, send the script and i will test it
Its actually this, I did a little bit of edit on it it might work. Or will probably stay the same as the other one I worked on before.
Its the one I sent you, If you didn’t know.