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

Can Someone Explain me This Little Piece of Code?

Asked by
Borrahh 265 Moderation Voter
4 years ago

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
0
You created a function, with parameters. Since you inserted the sound into the parameter, the function did its job. Simple as that. torchmaster101 92 — 4y
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 Borrahh 265 — 4y

2 answers

Log in to vote
3
Answered by 4 years ago

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

Ad
Log in to vote
0
Answered by 4 years ago
--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

0
Thanks! Borrahh 265 — 4y

Answer this question