How can I make my script prevent your mouse from leaving the window?

GetMouseLocation
Vector2
This function returns a Vector2 representing the current screen location of the player’s Mouse in pixels relative to the top left corner. This does not account for the GUI inset.
If the location of the mouse pointer is offscreen or the players device does not have a mouse, the value returned will be undetermined instead of Vector2.
As UserInputService is client-side only, this function can only be used in a LocalScript.

According to the docs, if you use GetMouseLocation, and it returns nil, it is either out of the viewport, or the user does not have a mouse connected. Just check that the player is on a computer and it should be good. (ill find an article I had ages ago for finding the device)

edit: Device Type Detection - #46 by Maelstronomer