Advertisement
TheRealAaron

Roblox Shift Lock GUI: Enhanced Shift Lock Functionality

May 8th, 2023 (edited)
3,150
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.19 KB | None | 0 0
  1. -- Instances:
  2. local ShiftlockStarterGui = Instance.new("ScreenGui")
  3. local ImageButton = Instance.new("ImageButton")
  4. -- Properties:
  5. ShiftlockStarterGui.Name = "ShiftlockStarterGui"
  6. ShiftlockStarterGui.DisplayOrder = 1000
  7. ShiftlockStarterGui.ResetOnSpawn = false
  8. ShiftlockStarterGui.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")
  9. ShiftlockStarterGui.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
  10. ImageButton.Name = "ShiftLockButton"
  11. ImageButton.BackgroundTransparency = 1
  12. ImageButton.AnchorPoint = Vector2.new(1, 0.5)
  13. ImageButton.Position = UDim2.new(1, -10, 0.5, 0)
  14. ImageButton.Size = UDim2.new(0, 36, 0, 36)
  15. ImageButton.Image = "rbxassetid://6996350026" -- Replace with your own shift lock icon image ID
  16. -- Code:
  17. local ShiftLockController = require(script.ShiftLockController)
  18. local function EnableShiftLock()
  19.     ShiftLockController:SetShiftLockEnabled(true)end
  20. local function DisableShiftLock()
  21.     ShiftLockController:SetShiftLockEnabled(false)
  22. end
  23. local function ToggleShiftLock()
  24.     if ShiftLockController:IsShiftLockEnabled() then
  25.         DisableShiftLock()
  26.     else
  27.         EnableShiftLock()
  28.     end
  29. end
  30. ImageButton.MouseButton1Click:Connect(ToggleShiftLock)
  31. -- Key binding:
  32. local UserInputService = game:GetService("UserInputService")
  33. local shiftLockActionName = "ToggleShiftLock"
  34. local shiftLockAction = Enum.KeyCode.LeftShift
  35. local isShiftKeyPressed = false
  36. local function OnInputBegan(input, isProcessed)
  37.     if isProcessed then
  38.         return
  39.     end
  40.    
  41.     if input.KeyCode == shiftLockAction then
  42.         isShiftKeyPressed = true
  43.         ToggleShiftLock()
  44.     end
  45. end
  46. local function OnInputEnded(input, isProcessed)
  47.     if isProcessed then
  48.         return
  49.     end
  50.    
  51.     if input.KeyCode == shiftLockAction then
  52.         isShiftKeyPressed = false
  53.         if not UserInputService:IsKeyDown(shiftLockAction) then
  54.             DisableShiftLock()
  55.         end
  56.     end
  57. end
  58. UserInputService.InputBegan:Connect(OnInputBegan)
  59. UserInputService.InputEnded:Connect(OnInputEnded)
  60. -- Update shift lock button visibility based on device:
  61. local function UpdateButtonVisibility()
  62.     local isTouchDevice = UserInputService.TouchEnabled
  63.     ImageButton.Visible = not isTouchDevice
  64. end
  65. UpdateButtonVisibility()
  66. UserInputService.TouchEnabledChanged:Connect(UpdateButtonVisibility)
Advertisement
Comments
  • TheRealAaron
    2 years
    # text 0.83 KB | 0 0
    1.  
    2. In this improved version:
    3. - The `ShiftlockStarterGui` is given additional properties like `DisplayOrder` and `ResetOnSpawn` for better control.
    4. - The shift lock icon image is set using a valid asset ID. Replace it with your own image ID.
    5. - The `ShiftLockController` functionality is moved to a separate module script for better organization and reusability.
    6. - The shift lock functionality is encapsulated within the `ShiftLockController` module.
    7. - The shift lock button's click event is connected directly to the `ToggleShiftLock` function, making the code cleaner.
    8. - The shift lock functionality is bound to the LeftShift key input event for convenience.
    9. - The shift lock button's visibility is updated based on the user's device (touch-enabled or not).
    10.  
    11. Feel free to modify and further enhance the code to fit your specific requirements.
Add Comment
Please, Sign In to add comment
Advertisement