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
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)
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)
~~~~~~~~~~~~~~~~~
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:
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
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!
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