First, what does
Ifx ( long )
do? What is this testing for exactly and how is it different than
If ( long == 0 )
I've seen the same script use both. I guess worded differently what is Ifx for and why would you use If for a long that is going to be set as a reference?
MWSE functions won't work in normal ifs or whiles, you have to use ifx or whilex. These are limited in what they can do (e.g. you can't say "ifx ( ref->xGetItemCount var > othervar )" or anything like that, it won't work), and there's no elseif, so you have to do some odd workarounds sometimes.
So ifx ( var ) is for when you want to use MWSE functions if var != 0. Much the same with whilex.
You can use normal if/while and of course normal functions inside an ifx/whilex, but not the other way round.
Hope that makes sense.
Second, when you use
xsetref long
how long does that last? Does it only work for the line right after it? How come many scripts that I am examining have it spammed so much?
Basically yes, it's just the next function that operates on that reference. It's used when there's no extended function that can take a ref var that can do what you want, so e.g. if you want to disable something you'd use xSetRef, disable - but if you want to startcombat you'd use ref->xStartCombat ref instead since that's easier/clearer.
Third, if I set my long to an npc reference and I have determined that the npc reference has my spell effect if I start a new script like this
xsetref npcref
startscript spellscript2
Does everything in my new script affect the npc reference?
Yes, that makes "spellscript2" a targeted script on that reference, so it will default to that reference when it needs something to operate on. If you still need a ref var in the "spellscript2" script, you can either set it from the first script or use xRefID without specifying anything, which will get you the ref id of the script's target, and convert that to a ref using xGetRef.
What if I just did
startscript spellscript2
without the xetref is that completely different?
Yes, that would start another global script (as opposed to a targeted script), which would always need to specify the id/reference it's operating on (a targeted script can just say, e.g. SetPos x 512, and that will affect the script's target by default).
Also, is a freeze, not a CTD, a sign that a script is stuck in a loop and can't get out?
It can be, yes (it can mean any number of things, but an infinite loop is definitely one of them).
I've got it to where I can find the npc with the spell effect and start a secondscript but once I stop the second script it freezes. If I let the second script loop forever, so that the first script won't start the second again because of a check I have in the first, it doesn't freeze. What could that mean?
Sorry, not sure what you mean by that... If a script is already running you shouldn't try to start it again, if that's what you're doing (you can check if ScriptRunning to make sure)... Or if the script was already stopped then did you reset any variables that need resetting?