So I’ve got a hopefully easy question, but I don’t understand why my code isn’t doing what I want it to.
function Sound:load()
trackToPlay = musicDownbeat
trackToPlay:play()
end
function Sound:changeMusic()
if trackToPlay == musicUpbeat then
trackToPlay:stop()
trackToPlay = musicDownbeat
trackToPlay:play()
end
if trackToPlay == musicDownbeat then
trackToPlay:stop()
trackToPlay = musicUpbeat
trackToPlay:play()
end
end
So I’ve got two Source tracks that can be alternated between, musicUpbeat and musicDownbeat, and at this point in the code (I have stripped down Sound:load() to make it as clear as possible), every time changeMusic() is called, trackToPlay is always musicDownbeat, meaning that every time changeMusic() is called, the music stops and is changed to musicUpbeat.
Sound:load() is only called once, right? So why are my trackToPlay changes not being saved?
The problem is in the function
changeMusic. You need to useelseifinstead of twoifstatements. Your code should look like this:The way you have written it in your original code, if
trackToPlayismusicUpbeat(it will be afterchangeMusicis called the first time), it will be changed intomusicDownbeatby the first statement, and then immediately changed intomusicUpbeatby the secondifstatement.