Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a song play using a button?

Asked by 4 years ago
Edited 4 years 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!!

01print 'Loading'
02local button = script.Parent
03------------------------
04song1 = "http://www.roblox.com/asset/?id=2538961789" --paste wanted song ID
08 
09-----------------------------
10print 'Creating a Source of Audio'
11local music = Instance.new("Sound")
12music.Archivable = true
13print 'Inspecting the Configurable Variables'
14 
15 
View all 67 lines...
0
i just meant it made no sense bc it had no code block. im sure you're a great scripter, sorry! LiLFriks 39 — 4y

4 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
01local songs = {
06}
07local songLength = #songs              
08local Sound = script.Parent.Parent.Sound   
09local CD = script.Parent                                            
10local selected = 0                     
11CD.MouseClick:Connect(function()           
12    selected += 1                      
13    local chosen = selected%#songs     
14    if chosen == 0 then                
15        chosen = #songs
16    end
17    print(chosen)
18    Sound.SoundId = songs[chosen]      
19    Sound:Play()--playing the song
20    end)

0
This should work with any number of songs mikey2019d 43 — 4y
0
doesnt work :( LiLFriks 39 — 4y
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 43 — 4y
0
yes LiLFriks 39 — 4y
View all comments (22 more)
0
ok i will take a look again mikey2019d 43 — 4y
0
I found the problem Instead of a local script insert a script and paste the code u had in the local script mikey2019d 43 — 4y
0
i wasnt using a local script though LiLFriks 39 — 4y
0
does it output any errors? mikey2019d 43 — 4y
0
sound is not a valid member of workspace LiLFriks 39 — 4y
0
can u send me a screen shot of your explorer tab with the part expanded and the script? mikey2019d 43 — 4y
0
quick question is sound in side of click detector or is it just in the part? mikey2019d 43 — 4y
0
it looks like this LiLFriks 39 — 4y
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 — 4y
0
where is sound mikey2019d 43 — 4y
0
workspace LiLFriks 39 — 4y
0
put it in button mikey2019d 43 — 4y
0
still doesnt work LiLFriks 39 — 4y
0
I dont know then best of luck mikey2019d 43 — 4y
0
can I join your studio? mikey2019d 43 — 4y
0
sure LiLFriks 39 — 4y
0
send me alink mikey2019d 43 — 4y
0
yay it works ty LiLFriks 39 — 4y
0
Yo the model works just get that it should work mikey2019d 43 — 4y
0
does it work? mikey2019d 43 — 4y
0
No problem good luck in your scripting journey mikey2019d 43 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
01Your script is way to long.
02And it has lots of errors.
03I think this.
04 
05local music = Instance.new("Sound") -- music variable
06 
07local button = script.Parent -- the button
08 
09local clickDectector = button.ClickDectector -- the buttons click dectector
10 
11clickDectector.MouseClick:Connect(function() -- when the player clicks
12 
13 
14local sound1id = "put number"
15 
View all 39 lines...

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

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 — 4y
0
ok Trampyling 43 — 4y
0
DONE Trampyling 43 — 4y
0
does it work????? Trampyling 43 — 4y
View all comments (5 more)
0
doesnt work :( LiLFriks 39 — 4y
0
hmmmmmmmm Trampyling 43 — 4y
0
i changed my answer it will surely work i think Trampyling 43 — 4y
0
doesnt work still LiLFriks 39 — 4y
0
WHAT Trampyling 43 — 4y
Log in to vote
0
Answered by
Benbebop 1049 Moderation Voter
4 years ago
Edited 4 years 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:

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

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.

1local songs = {1234567, 7654321, 1325476}

{} constructs a table out of the provided values

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

1local songs = {1234567, 7654321, 1325476}
2 
3sound.Ended:Connect(function()
4    sound.SoundId = songs[]
5end)

[] 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.

01local songs = {1234567, 7654321, 1325476}
02 
03local iteration = 1
04 
05sound.Ended:Connect(function()
06    sound.SoundId = songs[iteration]
07    iteration = iteration + 1
08    if iteration >= #songs then
09        iteration = 1
10    end
11end)

# tells you how many values are in a table

Finally add sound:Play

01local songs = {1234567, 7654321, 1325476}
02 
03local iteration = 1
04 
05sound.Ended:Connect(function()
06    sound.SoundId = songs[iteration]
07    iteration = iteration + 1
08    if iteration >= #songs then
09        iteration = 1
10    end
11    sound:Play()
12end)

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

1clickdetector.MouseClick:Connect(function()
2    sound.SoundId = songs[iteration]
3    iteration = iteration + 1
4    if iteration >= #songs then
5        iteration = 1
6    end
7    sound:Play()
8end)

Now we squish these two scripts together and:

01local songs = {1234567, 7654321, 1325476}
02 
03local iteration = 1
04 
05sound.Ended:Connect(function()
06    sound.SoundId = songs[iteration]
07    iteration = iteration + 1
08    if iteration >= #songs then
09        iteration = 1
10    end
11    sound:Play()
12end)
13 
14clickdetector.MouseClick:Connect(function()
15    sound.SoundId = songs[iteration]
View all 21 lines...

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

01local songs = {1234567, 7654321, 1325476}
02 
03local iteration = 1
04 
05function iterateSong()
06    sound.SoundId = songs[iteration]
07    iteration = iteration + 1
08    if iteration >= #songs then
09        iteration = 1
10    end
11    sound:Play()
12end
13 
14sound.Ended:Connect(function()
15    iterateSong()
16end)
17 
18clickdetector.MouseClick:Connect(function()
19    iterateSong()
20end)

And finally were done, hope this helped!

0
doesnt work :( LiLFriks 39 — 4y
0
@LilFriks Does it give any errors? Benbebop 1049 — 4y
0
Workspace.button.song changer:12: Expected identifier when parsing expression, got ')' LiLFriks 39 — 4y
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 1049 — 4y
0
I fixed it in the post, so just look there for it. Benbebop 1049 — 4y
Log in to vote
0
Answered by
DemGame 271 Moderation Voter
4 years ago
Edited 4 years ago

This can be done much more efficiently:

01local song1 = "http://www.roblox.com/asset/?id=2538961789" --paste wanted song ID
04local   song4 = "http://www.roblox.com/asset/?id=2206587324"    
05 
06local soundblock = Instance.new("Sound")
07soundblock.Name = Music
08soundblock.Parent = script.Parent
09soundblock.Looped = true
10soundblock.Playing = false
11local songqueue = 1
12 
13local function PlayMusic()
14soundblock.Playing = true
15songqueue = songqueue + 1
View all 34 lines...
0
doesnt work either :(( LiLFriks 39 — 4y
0
?????? DemGame 271 — 4y
0
Any errors in output? DemGame 271 — 4y
0
Workspace.button.song changer:21: Expected 'then' when parsing if statement, got '=' LiLFriks 39 — 4y
View all comments (4 more)
0
ohhhhhhhhhhh let me edit it DemGame 271 — 4y
0
it still doesnt work; songs dont play at all LiLFriks 39 — 4y
0
hmmm DemGame 271 — 4y
0
anything in output? DemGame 271 — 4y

Answer this question