Yes, if a ship becomes stealthy, any target lock is instantly broken.
If you're unfamiliar with lua scripting and FreeSpace, I would read up on the
Lua Documentation, these
helpful tutorials that some studly person wrote, and look through the
scripting forum to see how other people have written scripts for FreeSpace.
With all that in mind, you can start to design a stealth system script. Start small and simple and slowly build your way up to a more complex solution.
Now if I were making the script, I'd have a lua table that kept track of any stealth ships,their stealth info, last time they fired a weapon etc etc and used that to activate or deactivate stealth flags.
I don't have any time to put something together but if you have any specific questions about something in lua I could try to help.