How do I find out what's going wrong with my script?

There are several techniques to debug scripts, or find out what the script is seeing in order to fix it.

Two of the simplest things to do this are to insert beeps (audible acknowledgements that all is or is not well), or to return information at crucial steps, either by way of a “return” command or by displaying a dialog.
Here is an example:

tell application "Finder"
	activate
	if exists folder "Images" then
		beep 2
	else
		display dialog "Folder does not exist." buttons "OK" default button "OK"
	end if
end tell

If you hear two beeps all is well, if not a dialog is presented.
Or consider this:

set prefs to path to preferences folder
display dialog prefs -- to see how the path is written by AppleScript

Using these two techniques are invaluable debugging tools. For step-by-step debugging, looks into Late Night Software’s Script Debugger. Take a look also to the invaluable debugging tools in Satimage Software’s Smile.