Still have questions? Join our Discord server and get real time help.
0

# How to make a song play using a button?

Edited 3 months ago

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"

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


0
i just meant it made no sense bc it had no code block. im sure you're a great scripter, sorry! LiLFriks 39 — 3mo

0
Edited 3 months ago
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)



0
This should work with any number of songs mikey2019d 38 — 3mo
0
doesnt work :( LiLFriks 39 — 3mo
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 mikey2019d 38 — 3mo
0
yes LiLFriks 39 — 3mo
0
ok i will take a look again mikey2019d 38 — 3mo
0
I found the problem Instead of a local script insert a script and paste the code u had in the local script mikey2019d 38 — 3mo
0
i wasnt using a local script though LiLFriks 39 — 3mo
0
does it output any errors? mikey2019d 38 — 3mo
0
sound is not a valid member of workspace LiLFriks 39 — 3mo
0
can u send me a screen shot of your explorer tab with the part expanded and the script? mikey2019d 38 — 3mo
0
quick question is sound in side of click detector or is it just in the part? mikey2019d 38 — 3mo
0
it looks like this LiLFriks 39 — 3mo
0
the part is named button, with a click detector and script inside of it. the part is in workspace and is unanchored. LiLFriks 39 — 3mo
0
where is sound mikey2019d 38 — 3mo
0
workspace LiLFriks 39 — 3mo
0
put it in button mikey2019d 38 — 3mo
0
still doesnt work LiLFriks 39 — 3mo
0
I dont know then best of luck mikey2019d 38 — 3mo
0
can I join your studio? mikey2019d 38 — 3mo
0
sure LiLFriks 39 — 3mo
0
send me alink mikey2019d 38 — 3mo
0
yay it works ty LiLFriks 39 — 3mo
0
Yo the model works just get that it should work mikey2019d 38 — 3mo
0
does it work? mikey2019d 38 — 3mo
0
No problem good luck in your scripting journey mikey2019d 38 — 3mo
0
Edited 3 months ago
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)


~~~~~~~~~~~~~~~~~

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 LiLFriks 39 — 3mo
0
ok Trampyling -13 — 3mo
0
DONE Trampyling -13 — 3mo
0
does it work????? Trampyling -13 — 3mo
0
doesnt work :( LiLFriks 39 — 3mo
0
hmmmmmmmm Trampyling -13 — 3mo
0
i changed my answer it will surely work i think Trampyling -13 — 3mo
0
doesnt work still LiLFriks 39 — 3mo
0
WHAT Trampyling -13 — 3mo
0
Benbebop 787
3 months ago
Edited 3 months ago

# 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

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!

0
doesnt work :( LiLFriks 39 — 3mo
0
@LilFriks Does it give any errors? Benbebop 787 — 3mo
0
Workspace.button.song changer:12: Expected identifier when parsing expression, got ')' LiLFriks 39 — 3mo
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. Benbebop 787 — 3mo
0
I fixed it in the post, so just look there for it. Benbebop 787 — 3mo
0
DemGame 126
3 months ago
Edited 3 months ago

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

0
doesnt work either :(( LiLFriks 39 — 3mo
0
?????? DemGame 126 — 3mo
0
Any errors in output? DemGame 126 — 3mo
0
Workspace.button.song changer:21: Expected 'then' when parsing if statement, got '=' LiLFriks 39 — 3mo