I can’t script well, so sorry if you’re already doing this, but are you checking if the “target square” is a real location to travel to?
You clamp your values at eight, so if a piece tried to move to, say, (7, 10), the value gets clamped and turned into (7,8), which is a real location, so it gets shown.
I believe to fix this, add a check whether the target square is possible to travel to, and if not, stop displaying squares across that line. I don’t believe this would need the clamp function