Advertisement
DrawingJhon

Shift Lock (Localscript)

Mar 25th, 2021 (edited)
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. local MobileCameraFramework = {}
  2. local players = game:GetService("Players")
  3. local runservice = game:GetService("RunService")
  4. local CAS = game:GetService("ContextActionService")
  5. local player = players.LocalPlayer
  6. local character = player.Character or player.CharacterAdded:Wait()
  7. local root = character:WaitForChild("HumanoidRootPart")
  8. local humanoid = character.Humanoid
  9. local camera = workspace.CurrentCamera
  10. local mouse = player:GetMouse()
  11. local button = {}
  12. local UserGameSettings = UserSettings():GetService("UserGameSettings")
  13. local DEFAULT_MOUSE_LOCK_CURSOR = "rbxasset://textures/MouseLockedCursor.png"
  14.  
  15. --Visiblity
  16. uis = game:GetService("UserInputService")
  17. ismobile = false --uis.TouchEnabled
  18. button.Visible = ismobile
  19.  
  20. local states = {
  21.     OFF = "rbxasset://textures/ui/mouseLock_off@2x.png",
  22.     ON = "rbxasset://textures/ui/mouseLock_on@2x.png"
  23. }
  24. local MAX_LENGTH = 900000
  25. local active = false
  26. local ENABLED_OFFSET = CFrame.new(1.7, 0, 0)
  27. local DISABLED_OFFSET = CFrame.new(-1.7, 0, 0)
  28. local function UpdateImage(STATE)
  29.     button.Image = states[STATE]
  30. end
  31. local function UpdateAutoRotate(BOOL)
  32.     humanoid.AutoRotate = BOOL
  33. end
  34. local function GetUpdatedCameraCFrame(ROOT, CAMERA)
  35.     return CFrame.new(root.Position, Vector3.new(CAMERA.CFrame.LookVector.X * MAX_LENGTH, root.Position.Y, CAMERA.CFrame.LookVector.Z * MAX_LENGTH))
  36. end
  37.  
  38. local function isFirstPerson()
  39.     local character = game:GetService("Players").LocalPlayer.Character
  40.     local head = character and character:findFirstChild("Head")
  41.     if head and head:IsA("BasePart") then
  42.         if (character.Head.CFrame.p - camera.CFrame.p).magnitude < 1 then
  43.             return true
  44.         else
  45.             return false
  46.         end
  47.     end
  48. end
  49.  
  50. local function EnableShiftlock()
  51.     if isFirstPerson() then return end
  52.     mouse.Icon = DEFAULT_MOUSE_LOCK_CURSOR
  53.     UpdateAutoRotate(false)
  54.     UpdateImage("ON")
  55.     UserGameSettings.RotationType = Enum.RotationType.CameraRelative
  56.     uis.MouseBehavior = Enum.MouseBehavior.LockCenter
  57.     root.CFrame = GetUpdatedCameraCFrame(root, camera)
  58.     camera.CFrame = camera.CFrame * ENABLED_OFFSET
  59. end
  60. local function DisableShiftlock()
  61.     mouse.Icon = ""
  62.     UpdateAutoRotate(true)
  63.     UpdateImage("OFF")
  64.     camera.CFrame = camera.CFrame * DISABLED_OFFSET
  65.     pcall(function()
  66.         active:Disconnect()
  67.         active = nil
  68.     end)
  69. end
  70. UpdateImage("OFF")
  71. active = false
  72. function ShiftLock()
  73.     if not active then
  74.         active = runservice.RenderStepped:Connect(function()
  75.             EnableShiftlock()
  76.         end)
  77.     else
  78.         DisableShiftlock()
  79.     end
  80. end
  81. local ShiftLockButton = CAS:BindAction("ShiftLOCK", ShiftLock, false, "On")
  82. CAS:SetPosition("ShiftLOCK", UDim2.new(0.8, 0, 0.8, 0))
  83. local AllowedToShiftLock = true
  84. uis.InputBegan:Connect(function(input)
  85.     if not AllowedToShiftLock then return end
  86.     if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
  87.         ShiftLock()
  88.     end
  89. end)
  90.  
  91. player.Chatted:Connect(function(msg)
  92.     msg = msg:gsub("^/e ", "")
  93.     if msg:lower() == "/enableshiftlock" then
  94.         AllowedToShiftLock = true
  95.         warn("Shift lock enabled")
  96.     elseif msg:lower() == "/disableshiftlock" then
  97.         AllowedToShiftLock = false
  98.         DisableShiftlock()
  99.         warn("Shift lock disabled")
  100.     end
  101. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement