Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a song play using a button?

Hi! I'm just learning how to script, and I'd like some guidance. So I have a click detector and a script in a part named "button" in workspace. The script makes music play that you can hear in-game. I'm trying to script it so that when the button is pressed, the next song starts playing, and no matter how many times the button is pressed, this just repeats.

Here's the script. The song change part I scripted is at the bottom, but it doesn't really work. I'm really not sure why... I've tried scripting it other ways but I was sure this would work. Any guidance would be greatly appreciated!!

print 'Loading'
local button = script.Parent
------------------------
song1 = "http://www.roblox.com/asset/?id=2538961789" --paste wanted song ID 
song2 = "http://www.roblox.com/asset/?id=4867633321"
song3 = "http://www.roblox.com/asset/?id=2231782710"
song4 = "http://www.roblox.com/asset/?id=2206587324"

-----------------------------
print 'Creating a Source of Audio'
local music = Instance.new("Sound")
music.Archivable = true 
print 'Inspecting the Configurable Variables'



music.Parent = script.Parent 
music.Pitch = 1 
music.Volume = 3
music.Looped = false 
music.Name = "Player"

print 'Loading RMP 1.0'
while (true) do
	print 'Playing Sound 1'
music.SoundId = song1 

music:play()
wait(130) 
	
	--2--
	print 'Playing Sound 2'
music.SoundId = song2
music:play() 
wait(130)
	--3--
	print 'Playing Sound 3'
music.SoundId = song3
music:play() 
wait(130)
	--4--
	print 'Playing Sound 4'
music.SoundId = song4
music:play() 
	wait(130)
	
local function OnClicked()
 music:Stop()
	music.SoundId = song1
	 music:Play()
		if song1.play == true then
		 music:Stop()
		music.SoundId = song2
		music:Play()
		elseif song2.play == true then
		 music:Stop()
		music.SoundId = song3
		music:Play()
		elseif song3.play == true then
		 music:Stop()
		music.SoundId =  song4
		music:Play()
	end
end
button.ClickDetector.MouseClick:connect(OnClicked)

end

1 comment

0

i just meant it made no sense bc it had no code block. im sure you're a great scripter, sorry!

4 answers

✓ Accepted answer
local songs = {
	"http://www.roblox.com/asset/?id=2538961789",
	"http://www.roblox.com/asset/?id=4867633321",
	"http://www.roblox.com/asset/?id=2231782710",
	"http://www.roblox.com/asset/?id=2206587324"
}
local songLength = #songs				
local Sound = script.Parent.Parent.Sound	
local CD = script.Parent					 						 
local selected = 0						
CD.MouseClick:Connect(function()			
	selected += 1						
	local chosen = selected%#songs		
	if chosen == 0 then 				
		chosen = #songs
	end
	print(chosen)
	Sound.SoundId = songs[chosen]		
	Sound:Play()--playing the song
	end)

26 comments

0

This should work with any number of songs

0

doesnt work :(

0

did u add a click detector inside a part then add a sound inside the part then add a local script inside of clickdetector

0

yes

0

ok i will take a look again

0

I found the problem Instead of a local script insert a script and paste the code u had in the local script

0

i wasnt using a local script though

0

does it output any errors?

0

sound is not a valid member of workspace

0

can u send me a screen shot of your explorer tab with the part expanded and the script?

0

quick question is sound in side of click detector or is it just in the part?

0

it looks like this

0

the part is named button, with a click detector and script inside of it. the part is in workspace and is unanchored.

0

where is sound

0

workspace

0

put it in button

0

still doesnt work

0

I dont know then best of luck

0

can I join your studio?

0

sure

0

send me alink

0

yay it works ty

0

Yo the model works just get that it should work

0

does it work?

0

No problem good luck in your scripting journey

Your script is way to long.
And it has lots of errors.
I think this.

local music = Instance.new("Sound") -- music variable

local button = script.Parent -- the button

local clickDectector = button.ClickDectector -- the buttons click dectector

clickDectector.MouseClick:Connect(function() -- when the player clicks


local sound1id = "put number"

 local sound2id = "put number"

 local sound3id = "put number"

 local sound4id = "put number"
 
--Custom settings
music.Volume =   1   -- Number here
music.SoundId = sound1id
-- plays the music
music:Play()
-- when it ends
while wait(0) do
wait(music.TimeLength)
music.SoundId = sound2id
music:Play()
wait(music.TimeLength)
music.SoundId = sound3id
music:Play()
wait(music.TimeLength)
music.SoundId = sound4id
music:Play()
end
end)


9 comments

0

i want more than one sound id though. i want it to play a new song out of those 4 in my script every time the button is clicked

0

ok

0

DONE

0

does it work?????

0

doesnt work :(

0

hmmmmmmmm

0

i changed my answer it will surely work i think

0

doesnt work still

0

WHAT

This can be done much more efficiently:

local song1 = "http://www.roblox.com/asset/?id=2538961789" --paste wanted song ID
local	song2 = "http://www.roblox.com/asset/?id=4867633321"
local	song3 = "http://www.roblox.com/asset/?id=2231782710"
local	song4 = "http://www.roblox.com/asset/?id=2206587324"	 

local soundblock = Instance.new("Sound")
soundblock.Name = Music
soundblock.Parent = script.Parent
soundblock.Looped = true
soundblock.Playing = false
local songqueue = 1

local function PlayMusic()
soundblock.Playing = true
songqueue = songqueue + 1
	if songqueue > 4 then
	songqueue = 1
end
button.ClickDetector.MouseClick:connect(PlayMusic)

while true do
	if songqueue == 1 then
		soundblock.SoundId = song1
	else if songqueue == 2 then
		soundblock.SoundId = song2
	else if songqueue == 3 then
		soundblock.SoundId = song3
	else if songqueue == 4 then
		soundblock.SoundId = song4
	else soundblock.SoundId = song1
			end
		end
	wait()
end

8 comments

0

doesnt work either :((

0

??????

0

Any errors in output?

0

Workspace.button.song changer:21: Expected 'then' when parsing if statement, got '='

0

ohhhhhhhhhhh let me edit it

0

it still doesnt work; songs dont play at all

0

hmmm

0

anything in output?

This could use a lot of cleaning up and shortening

though it seems like you have an understanding of everything you used so I wont go over each thing. First lets get this loop working. It seems like you want it to wait till the end of each song, so lets do that first.

Song cycle

Using the sound.Ended event you can check if the sound ended:

sound.Ended:Connect(function()
	-- play next song --
end)

event:Connect(function()) is functionally the same as function yourFunction just all on one line.

Now we can create a table with all of your sound ids/urls in it.

local songs = {1234567, 7654321, 1325476}

{} constructs a table out of the provided values

Now we can use the code we made before and combine them.

local songs = {1234567, 7654321, 1325476}

sound.Ended:Connect(function()
	sound.SoundId = songs[]
end)

[] should contain a number of which value in the table the script will use

We'll need something to iterate through the table and clip it when it reaches the limit.

local songs = {1234567, 7654321, 1325476}

local iteration = 1

sound.Ended:Connect(function()
	sound.SoundId = songs[iteration]
	iteration = iteration + 1
	if iteration >= #songs then
		iteration = 1
	end
end)

# tells you how many values are in a table

Finally add sound:Play

local songs = {1234567, 7654321, 1325476}

local iteration = 1

sound.Ended:Connect(function()
	sound.SoundId = songs[iteration]
	iteration = iteration + 1
	if iteration >= #songs then
		iteration = 1
	end
	sound:Play()
end)

Now that we covered that lets do the

Click detection

This part is mostly the same, just instead of using sound.Ended we use clickdetector.MouseClick

clickdetector.MouseClick:Connect(function()
	sound.SoundId = songs[iteration]
	iteration = iteration + 1
	if iteration >= #songs then
		iteration = 1
	end
	sound:Play()
end)

Now we squish these two scripts together and:

local songs = {1234567, 7654321, 1325476}

local iteration = 1

sound.Ended:Connect(function()
	sound.SoundId = songs[iteration]
	iteration = iteration + 1
	if iteration >= #songs then
		iteration = 1
	end
	sound:Play()
end)

clickdetector.MouseClick:Connect(function()
	sound.SoundId = songs[iteration]
	iteration = iteration + 1
	if iteration >= #songs then
		iteration = 1
	end
	sound:Play()
end)

Though even still this could be shorter, instead of repeating all that code we can just use a function.

local songs = {1234567, 7654321, 1325476}

local iteration = 1

function iterateSong()
	sound.SoundId = songs[iteration]
	iteration = iteration + 1
	if iteration >= #songs then
		iteration = 1
	end
	sound:Play()
end

sound.Ended:Connect(function()
	iterateSong()
end)

clickdetector.MouseClick:Connect(function()
	iterateSong()
end)

And finally were done, hope this helped!

5 comments

0

doesnt work :(

0

@LilFriks Does it give any errors?

0

Workspace.button.song changer:12: Expected identifier when parsing expression, got ')'

0

@LiLFriks Apologies for the long response time, I don’t often check this. Yea the only reason then is because I left a “)” next to the first “end”, delete that and it should work fine.

0

I fixed it in the post, so just look there for it.

This discussion is preserved as it was. New answers, comments, and votes are closed.