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!!
01 | print 'Loading' |
02 | local button = script.Parent |
03 | ------------------------ |
04 | song 1 = "http://www.roblox.com/asset/?id=2538961789" --paste wanted song ID |
05 | song 2 = "http://www.roblox.com/asset/?id=4867633321" |
06 | song 3 = "http://www.roblox.com/asset/?id=2231782710" |
07 | song 4 = "http://www.roblox.com/asset/?id=2206587324" |
08 |
09 | ----------------------------- |
10 | print 'Creating a Source of Audio' |
11 | local music = Instance.new( "Sound" ) |
12 | music.Archivable = true |
13 | print 'Inspecting the Configurable Variables' |
14 |
15 |
01 | local songs = { |
06 | } |
07 | local songLength = #songs |
08 | local Sound = script.Parent.Parent.Sound |
09 | local CD = script.Parent |
10 | local selected = 0 |
11 | CD.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 ) |
01 | Your script is way to long. |
02 | And it has lots of errors. |
03 | I think this. |
04 |
05 | local music = Instance.new( "Sound" ) -- music variable |
06 |
07 | local button = script.Parent -- the button |
08 |
09 | local clickDectector = button.ClickDectector -- the buttons click dectector |
10 |
11 | clickDectector.MouseClick:Connect( function () -- when the player clicks |
12 |
13 |
14 | local sound 1 id = "put number" |
15 |
1 |
~~~~~~~~~~~~~~~~~
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.
Using the sound.Ended event you can check if the sound ended:
1 | sound.Ended:Connect( function () |
2 | -- play next song -- |
3 | 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.
1 | 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.
1 | local songs = { 1234567 , 7654321 , 1325476 } |
2 |
3 | sound.Ended:Connect( function () |
4 | sound.SoundId = songs [ ] |
5 | 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.
01 | local songs = { 1234567 , 7654321 , 1325476 } |
02 |
03 | local iteration = 1 |
04 |
05 | sound.Ended:Connect( function () |
06 | sound.SoundId = songs [ iteration ] |
07 | iteration = iteration + 1 |
08 | if iteration > = #songs then |
09 | iteration = 1 |
10 | end |
11 | end ) |
#
tells you how many values are in a table
Finally add sound:Play
01 | local songs = { 1234567 , 7654321 , 1325476 } |
02 |
03 | local iteration = 1 |
04 |
05 | sound.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() |
12 | end ) |
Now that we covered that lets do the
This part is mostly the same, just instead of using sound.Ended we use clickdetector.MouseClick
1 | clickdetector.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() |
8 | end ) |
Now we squish these two scripts together and:
01 | local songs = { 1234567 , 7654321 , 1325476 } |
02 |
03 | local iteration = 1 |
04 |
05 | sound.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() |
12 | end ) |
13 |
14 | clickdetector.MouseClick:Connect( function () |
15 | sound.SoundId = songs [ iteration ] |
Though even still this could be shorter, instead of repeating all that code we can just use a function.
01 | local songs = { 1234567 , 7654321 , 1325476 } |
02 |
03 | local iteration = 1 |
04 |
05 | function iterateSong() |
06 | sound.SoundId = songs [ iteration ] |
07 | iteration = iteration + 1 |
08 | if iteration > = #songs then |
09 | iteration = 1 |
10 | end |
11 | sound:Play() |
12 | end |
13 |
14 | sound.Ended:Connect( function () |
15 | iterateSong() |
16 | end ) |
17 |
18 | clickdetector.MouseClick:Connect( function () |
19 | iterateSong() |
20 | end ) |
And finally were done, hope this helped!
This can be done much more efficiently:
01 | local song 1 = "http://www.roblox.com/asset/?id=2538961789" --paste wanted song ID |
02 | local song 2 = "http://www.roblox.com/asset/?id=4867633321" |
03 | local song 3 = "http://www.roblox.com/asset/?id=2231782710" |
04 | local song 4 = "http://www.roblox.com/asset/?id=2206587324" |
05 |
06 | local soundblock = Instance.new( "Sound" ) |
07 | soundblock.Name = Music |
08 | soundblock.Parent = script.Parent |
09 | soundblock.Looped = true |
10 | soundblock.Playing = false |
11 | local songqueue = 1 |
12 |
13 | local function PlayMusic() |
14 | soundblock.Playing = true |
15 | songqueue = songqueue + 1 |