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

Can Someone Explain me This Little Piece of Code?

Basically, This code works. I am little bit confused on how it works, can someone add comments beside every line (expect the ones where I have defined the Music)

local MusicOne = game.SoundService.MusicOne
local MusicTwo = game.SoundService.MusicTwo
local MusicThree = game.SoundService.MusicThree
local MusicFour = game.SoundService.MusicFour
local MusicFive = game.SoundService.MusicFive
local function play(sound)
  sound:Play()
  sound.Ended:Wait()
end
while true do
  play(MusicOne)
  play(MusicTwo)
  play(MusicThree)
  play(MusicFour)
  play(MusicFive)
end

2 comments

0

You created a function, with parameters. Since you inserted the sound into the parameter, the function did its job. Simple as that.

0

This is not for an answer I was looking for, If you would love to help as I Said, pls go more on details

2 answers

✓ Accepted answer

Simply imagine anything that you put in the place of the parameter sound, ends up replacing it in the code.

local MusicOne = game.SoundService.MusicOne
local MusicTwo = game.SoundService.MusicTwo
local MusicThree = game.SoundService.MusicThree
local MusicFour = game.SoundService.MusicFour
local MusicFive = game.SoundService.MusicFive


local function play(sound) -- Here you have defined a function called play, and defined the parameter 'sound' that you will input later

	sound:Play() -- When you enter the name of a sound (e.g. MusicOne), it replaces the parameter, so it's the same as doing MusicOne:Play()

	sound.Ended:Wait() -- Once the song has ended, the function waits

end

while true do -- This is the beginning of an infinite loop. Any code in this loop will be repeated forever

	play(MusicOne) -- You are substituting 'sound' for MusicOne like in the example.

	play(MusicTwo) -- Once MusicOne ends, MusicTwo plays...

	play(MusicThree) -- And so on...

	play(MusicFour)

	play(MusicFive) -- After this song finishes, you start again from MusicOne

end

--These five lines define sounds already set up in the sound service (I think) 
local MusicOne = game.SoundService.MusicOne
local MusicTwo = game.SoundService.MusicTwo
local MusicThree = game.SoundService.MusicThree
local MusicFour = game.SoundService.MusicFour
local MusicFive = game.SoundService.MusicFive

--This function takes in one parameter, the sound (one of the ones above)
local function play(sound)
  sound:Play() --This plays the sound
  sound.Ended:Wait() --This waits until the sound finishes
end

--Loops infinitely 
while true do
  --Runs the function, play, for each sound
  play(MusicOne)
  play(MusicTwo)
  play(MusicThree)
  play(MusicFour)
  play(MusicFive)
end

This is a basic overview of what happens with this script. If you would like more explanation on the sound.Ended:Wait() part I have a video on my YouTube Channel all about events right here

1 comment

0

Thanks!

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