[SOLVED] - See end of post.

Dear forum,

I know I'm a new user and I know what this question usually requires to fix it; an attempt to call nil is an error in - probably - the definition of what you're trying to do; i.e. you're calling a non-existing function, command or variable.

However, I've been searching low and high to try and find the solution to this problem, but the internet has proven itself a little unhelpful. I was hoping someone here could help me figure this out.

I have an Advanced Computer linked up to an Iron Note Block from MiscPeripherals 3.1 on its 'top' side. My ComputerCraft version is 1.481 (as it is when it comes with the FeedTheBeast modpack). I was trying to play a sequence of notes from the note block, but my computer comes up with the 'attempt to call nil' message. Here is my code:

Spoiler
--playNote(INSTR,NOTE)
--0 piano; 1 bass drum; 2 snare; 3 click; 4 bass gt
--0-24 notes F#3-F#5

--Where in the song the computer is.
sequence = 1
--What note to play; sequence is used as an index
--for the table.
song = {
  0;
  1;
  2;
  3;
  4
}

--Now for the music playing:
--First, check whether you're receiving a redstone
--signal, i.e. whether it is nighttime.
--Then, play the next note in the sequence of the
--song. For this, increment the sequence variable
--until it is as long as the table 'song' has
--entries for. Then, revert to position 1 to loop.
--At any point, when the dawn sets in, stop the
--music and open the door.

function playMusic()

  playNote(0,song[sequence]);
  sequence = sequence+1;

  if sequence > #song then
	sequence = 1;
  end

end


if rs.getInput("right") then
  playMusic()
end

while true do end

I get an 'attempt to call nil' error in line 27, where this line is:
playNote(0,song[sequence]);

According to this post, I'm supposed to use the 'playNote(INSTRUMENT, NOTE)' command as I have done in my program, but the error leads me to believe that that is in fact not correct, or that I made a mistake somewhere.

It might not be functional at all yet at other parts, of course, as I was just setting it up to see if it would work. However, the attempt to call nil error came up and stopped my progress.

Thank you very much if you can help me!



—–
EDIT:
It took me a lot of tinkering and looking up things here and there, but eventually I found out what the problem was myself. The command
playNote(0,song[sequence]);
is not a valid one! To use this correctly, you must use the peripheral API built in to computercraft. See this webpage for more information:
http://computercraft...heral_%28API%29

This is what I changed:
playNote(0,song[sequence]);
is now

peripheral.call("top","playNote",0,song[sequence])

Also, the while true do end loop I had at the end is something I cannot recommend! It crashes your computer instantly. Instead, use something along the lines of

while true do
  playMusic()
end
Or even better

while running=true do
  playMusic()
end
(where 'running' is a defined variable used to stop the program from executing)



Thanks anyway!