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

I believe that this other method I proposed may be the closest you can get to achieving what you want with the panning.

Ill write up a bit of code related to it

local ViewportSize = workspace.CurrentCamera.ViewportSize 
workspace.CurrentCamera:GetPropertyChangedSignal("ViewportSize"):Connect(function()
    ViewportSize = game.Workspace.CurrentCamera.ViewportSize -- Incase they change the size
end

while true do -- Change to whichever looping thing you were using
    local pos = UIS:GetMouseLocation()
    if pos.X == 0 then
        print("Left")
    elseif pos.X == ViewportSize.X then
        print("Right")
    elseif pos.Y == 0 then
        print("Top")
    elseif pos.Y == ViewportSize.Y then
        print("Bottom")
    end
    task.wait()
end

Pan according to which if statement runs. If you want to be able to go diagonally, just make them all seperate if statements or add more if statements for combinations.