Let's go through the logic of the code...The rest script has three events Starting, completed and cancelled. The each event fires the script so the script fires at least two times per rest.
First it stores the hit points of the player resting when they start resting as a local variable on the player.....
When the rest is complete:
Gets the difference between the rested hit points, which by normal BW coding is fully recovered, and the pre rest hit points.
if the current hit points are greater than the pre rest hit points
sets damage variable to the difference.
retrieves the hit dice of the pc and reduces the damage variable by that amount
if the pc has a constitution bonus, reduces damage variable by that amount
if the damage stored is a positive number greater than 0, since the damage could be a negative number if level plus con bonus may be more than damage difference, apply the damage to the player...
I see no error in logic......Were they the same pc, were they the same sevel and had the same con bonus? Did they have equiped con bonus items? These can affect the amount of damage done to a pc to set hit points after rest.
Btw, I don't remember if cancelling rest does a partial heal on pc,s. Test it and if it does, I need to modify code for that possible exploit. Let me know.