Kuwaga: The lua scripts never actually make the emulator frameadvance. Instead, the emulator runs the scripts once per frame, and does not continue before they are finished for this frame. The emulator does something like this:
while(true)
{
call lua's beforeemulation hooks
emulate a frame
call lua's afteremulation hooks
if(displayframe)
{
call lua's graphics hooks
update screen
}
call main lua function
}
Note that these calls do not happen in parallel, but one after another, so that the emulator does not do anything while lua is running, but only continues when lua returns (by calling "emu.frameadvance" in the case of the lua main function). In the case of multiple scripts, the emulator calls the first one, which does something and then returns by calling emu.frameadvance. When the emulator gets back control, it calls the next one, and so on. So emu.frameadvance does not advance the frame, it simply says "I'm done for now, continue doing whatever you were doing".
DarkKobold: If you look at the pseudocode I posted above, you will probably see the cause of your problem. The lua main loop is called after the screen has been updated, but before the next frame has been emulated. So at this point, your script has access to the previous frame's state, but anything you draw will appear the next frame, and thus effectively lag one frame. To avoid this problem, you can use the other lua hooks. Afteremulation, for example.