Music system / random song picker code problem?

Hey,
I’m working on a music system with a mute button and a random sound picker. My problem is that I have to click the mute button once to play a song. This song is always the same, but I want to pick a random sound from “SoundService”. What do I have to change? I’m thankful for every answer. I’m not that good at scripting so I couldn’t solve the problem myself.




I hope someone finds the issue.

– IchbinFelix

1 Like

Try this:

local Sounds = game:GetService("SoundService"):GetChildren() -- Fetch list (table) of sounds
local RandomSound = Sounds[math.random(1, #Sounds)] -- Get random sound

Also I’d recommend posting the actual code here, embedded in ``` marks.


I only gave the code to find a random sound object.

You’re better of using folders to make your hierarchy neater. You should also try making a table with different sound IDs. For example:

local Sounds = {
    14346346,
    34563546,
    46123123
}

Quick notes

Your indexing is wrong and by typing game.SoundService.Sound.Playing = true, you’re only playing the first Sound object in SoundService. Instead, play the random sound variable.

3 Likes

Thank you for your help, but I don’t know what scripts I have to use “Local or Normal” and where should I put them. Into the Folder at “Sound Service”?

– IchbinFelix

It depends on how you want the sound to work. If you want there to be a random sound for each player, you should use a localscript. If you want to do the sound logic once and it be the same for every player, use a regular script.

If the script is local, the random sound will be different for each player.

If it is a regular script, it will be random but the same for each player.

The code suggested requires that the sounds are placed in SoundService, without any folders. However, if you want to put them in a folder, as suggested rightly, you need to modify the first line.

For example, if you put them in a folder called “Music”, the line should read:

local Sounds = game:GetService("SoundService").Music:GetChildren()

It’s situations like this where you should head down to the Developer Hub to try learning a bit more about what you’re working with. Most of the issues you have aren’t as difficult as you think, they’re mostly just conceptual and application problems.

In the future, please do include your code in code blocks on the thread itself as opposed to images. Showing images of the hierarchy is fine though but it’d be better to upload them directly rather than from a link. With PrntScr, you can select Copy on the right mouse context menu to save the image, which you can then paste into your thread. It’ll upload and display itself after.

1 Like

I could get it done Idk why it’s playing the same song over and over again. Maybe it has something to do with the mute script:

local sound = true

script.Parent.MouseButton1Click:Connect(function()
	if sound then
		script.Parent.Text = "Music: Off"
		sound = false
		game.SoundService.Sounds.Sound.Playing = false
	elseif not sound then
		script.Parent.Text = "Music: On"
		sound = true
		game.SoundService.Sounds.Sound.Playing = true
	end

end)

It’s not randomizing the songs.
localisation:
Screenshot_1
randomize script:

local Sounds = game:GetService("SoundService").Music:GetChildren() 
local RandomSound = Sounds[math.random(1, #Sounds)] 

Maybe we could contact each other via Roblox or disc to solve this issue. I’ll fine you with a little payment as “Thank you” via. group funds.
Felix🌹#4585

or. IchbinFelix

– IchbinFelix

Strap yourself in, because this is gonna be a long reply

Check that the sound is not set to looped. Apart from that, unless there’s something else controlling sound, it shouldn’t play over and over.

Code Issues

Your code has a few issues.

  1. Playing sound using properties: You should always use :play and :pause when playing sound, instead of using the properties. Granted, they do the same thing, but the methods are better. Change the lines which set the “playing” property of the current sound to use :play and :pause

  2. You don’t use the random variable in your code: The variable you use to select the random song is never used. You need it to be in the same script. To use it, you are going to need to shift around your codebase. As the changes required are quite large, I’ll put them underneath in their own section.

  3. Incorrect script type: The script inside the mute button needs to be a localscript.

Fixing your code

I’m assuming you want to have a different song picked and played after the one selected is done. I’ll add that in for you.

I won’t post complete code, because then you won’t learn anything. However, I will give you steps to completion.

Before you start, please read the articles on audio playback on the Developer Hub. There are some simple things you overlooked which you should really learn before trying this again.

Step 1

Rename each sound in the SoundService from 1, to how many there are. This will be vital later on.

So, if there are 10 sounds, they should be named from 1 to 10.

Step 2

Move your code from your randomising script into the mute script. They are needed there. Ensure that they come after the sound variable.

Step 3

Next to the sound variable, add a new one called “CurrentSound”. We’re going to store the sound to play here. Set it to nil.

Step 4

Change the sound played in the “script.Parent.MouseButton1Click” event so that it is the CurrentSound variable we just made. This makes it so that we mute the sound which is currently playing, not just a single sound which may not be playing.

Step 5

Enclose your randomisation script in an infinite loop, but change a single thing. On the line which sets the random song variable:

local RandomSound = Sounds[math.random(1, #Sounds)]

Change it so that it sets CurrentSound instead. Be sure to remove the local keyword!

FYI, and infinite loop looks like this:

while true do
    -- Do stuff here
end

Step 6

Near the end of the infinite loop, add the following code:

CurrentSound.Ended:Wait()

This makes sure we wait until the song ended, before we select a new one.

Step 7

Move the part where you connect events to after you declare the “CurrentSound” variable, or they will never run due to the loop.


Make all of these changes and post the script you now have so that I can check it over. Also, try it out. Tell me any errors you get. I think this should work, but my instructions may have some errors on my part.

That’s not needed. I do this kind of thing for fun. Don’t worry about rewarding me.

Mic Drop

3 Likes

I didn’t understand these parts:

game.SoundService.Sounds.Sound:play

Step 3

Next to the sound variable, add a new one called “CurrentSound”. We’re going to store the sound to play here. Set it to nil.

Step 4

Change the sound played in the “script.Parent.MouseButton1Click” event so that it is the CurrentSound variable we just made. This makes it so that we mute the sound which is currently playing, not just a single sound which may not be playing.

I didn’t get any errors. Here’s the current code:

local sound = true
while true do
local Sounds = game:GetService("SoundService").Music:GetChildren() -- Fetch list (table) of sounds
CurrentSound = Sounds[math.random(1, #Sounds)] -- Get random sound
CurrentSound.Ended:Wait()
end
local Sounds = {
    1099730877,
    858731188,
    1308037387,
	2375621580,
	1258778698,
	1006769336,
	1652297595,
	819012082
}


script.Parent.MouseButton1Click:Connect(function()
	if sound then
		script.Parent.Text = "Music: Off"
		sound = false
		game.SoundService.Sounds.Sound.Playing = false
	elseif not sound then
		script.Parent.Text = "Music: On"
		sound = true
		game.SoundService.Sounds.Sound.Playing = true
	end

end)

Thank you again for helping me to solve this difficult problem. Normally I don’t write scripts. This was my first time.

–IchbinFelix

You seem to have gotten a mash up of previous responses and messed a few things up.

Number 1

You are using two methods of loading the sound, which will not work.

Remove this section:

local Sounds = {
    1099730877,
     858731188,
     1308037387,
  2375621580,
     1258778698,
  1006769336,
  1652297595,
      819012082
}

(The indentation has gone all weird, not sure why)

Number 2

To complete my step 3, you need to add a new variable called CurrentSound after the variable Sound.

So add this code after that variable:

local CurrentSound = nil

Number 3

To complete my step 4, you need to now use the current sound variable in your event connection. So, change the lines which stop the sound playing to use the variable. Like this:

CurrentSound.Playing = false

You need to complete the other one as well.

Number 4

Move your event connections so that they are above the infinite loop.

So the part using script.parent.mousebutton1click:Connect(function() should be above while true do

Ignore it for now. We want to get things working before we think about code style.

Finally, please make sure that you have:

  1. Changed this to a localscript, as I said

  2. Changed the names of the sounds

  3. Put the localscript inside the mute button

(I’m on mobile right now please excuse spelling/grammar errors)

1 Like

Thats how the script turned out:

local sound = true
local CurrentSound = nil
script.Parent.MouseButton1Click:Connect(function()
while true do
local Sounds = game:GetService("SoundService").Music:GetChildren() -- Fetch list (table) of sounds
CurrentSound = Sounds[math.random(1, #Sounds)] -- Get random sound
CurrentSound.Ended:Wait()
end


if sound then
		script.Parent.Text = "Music: Off"
		CurrentSound.Playing = false
		game.SoundService.Sounds.Sound.Playing = false
	elseif not sound then
		script.Parent.Text = "Music: On"
		CurrentSound.Playing = true
		game.SoundService.Sounds.Sound.Playing = true
	end

end)

I hope I did everything correctly. At least it’s not showing me any errors.

  1. Changed this to a localscript, as I said (done)
  2. Changed the names of the sounds (done)
  3. Put the localscript inside the mute button (done)

The part which went inside of the script.parent.mousebutton1click:Connect(function() needs to move too.

And that includes the end) at the end.

1 Like

Should it look like that?

local sound = true
local CurrentSound = nil
script.Parent.MouseButton1Click:Connect(function()
if sound then
		script.Parent.Text = "Music: Off"
		CurrentSound.Playing = false
		game.SoundService.Sounds.Sound.Playing = false
	elseif not sound then
		script.Parent.Text = "Music: On"
		CurrentSound.Playing = true
		game.SoundService.Sounds.Sound.Playing = true
	end

end)

while true do
local Sounds = game:GetService("SoundService").Music:GetChildren() -- Fetch list (table) of sounds
CurrentSound = Sounds[math.random(1, #Sounds)] -- Get random sound
CurrentSound.Ended:Wait()
end

Didn’t got any errors btw.

I think you may have changed the wrong thing inside the event connection. It should read like this

script.Parent.Text = "Music: Off"
sound = false
CurremtSound.Playing = false

(Or true in the other part of the if statement)

This makes it so that the sound variable tells our code to mute, and also stops the current sound from playing.

Also, I forgot something.

In the final part (inside the loop), make sure you add the following code:

if sound then
    CurrentSound:Play()
end

Add that after the line reading

CurrentSound = Sounds[math.random(1, #Sounds)] -- Get random sound

This makes sure that we play the new sound, but only if we haven’t muted it.

1 Like

I changed everything:

local sound = true
local CurrentSound = nil
script.Parent.MouseButton1Click:Connect(function()
if sound then
		script.Parent.Text = "Music: Off"
		sound = false
		CurrentSound.Playing = false
		game.SoundService.Sounds.Sound.Playing = false
	elseif not sound then
		script.Parent.Text = "Music: On"
		sound = true
		CurrentSound.Playing = true
		game.SoundService.Sounds.Sound.Playing = true
	end

end)

while true do
local Sounds = game:GetService("SoundService").Music:GetChildren() -- Fetch list (table) of sounds
CurrentSound = Sounds[math.random(1, #Sounds)] -- Get random sound
if sound then
   CurrentSound:Play()
end
CurrentSound.Ended:Wait()
end

Is this everything? I tried it out “With Play” and I didn’t hear music. Sorry for asking to much but I’m not a good scirpter at all.

–IchbinFelix

1 Like

You are indexing .Music of soundService when trying to pick songs out of .Sounds.

2 Likes

You also have an extra line at line 8 and 13. Delete those and follow @REALTimothy0812’s correction.

3 Likes

Thanks for the help guys! @REALTimothy0812 / @CodeNinja16
I’m so proud of that community.

1 Like