I've created a new script.

merlin()
Exit
Function merlin()
break On
$merlin_path = "%windir%\msagent\chars\merlin.acs"
$agent = createobject ("agent.control.2")
$agent.connected = "1"
$= $agent.characters.load("merlin","$merlin_path")
$merlin = $agent.characters.character("merlin")
$= $merlin.moveto(400,200)
$= $merlin.show()
$wait = $merlin.play("Announce" )
do sleep 1 until $merlin.wait($wait) <> 0
$wait = $merlin.speak("What the?" )
do sleep 3 until $merlin.wait($wait) <> 0
?"script done..."
EndFunction

This script is giving the same error.
If I remove the function and endfunction and call this script from the login, it works. I was just hoping to keep it all in one script so that I can encrypt it so it doesn't get tampered.