Maybe you could just stop the character instead of changing the speed.
Stuff you would need for the solution Sebastian suggested.
You should be able to get the current way borders with this line
game.CurrentScene.SceneCurrentWaySystem.WaySystemBorder
And you can get the current cursor position with the getCursorPos() function
getCursorPos().x
getCursorPos().y
Then there's this built in function for checking point inside polygon
So the whole script might look something like this
polygon = game.CurrentScene.SceneCurrentWaySystem.WaySystemBorder
if not isPointInsidePolygon({ x = getCursorPos().x, y = getCursorPos().y}, polygon) then
--your actions
end
I didn't test it, don't know if that works.