3DCreator

CursorModule

Jan 1st, 2025 (edited)
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 KB | None | 0 0
  1. -- Module to replace the built in MouseEnter and MouseLeave functions with a position argument. Which I wasted 3 hours on because last time I used the built in functions it was extremely reliable than when I finished this I decided to test the built in functions for the first time in 2 yrs js to found out it was way way more reliable now so I decided to put my custom one and the built in one side by side and mine was like 1% more reliable on computer so no difference basically.
  2.  
  3. local UserInputService = game:GetService("UserInputService")
  4. local GuiService = game:GetService("GuiService")
  5. local RunService = game:GetService("RunService")
  6. local Players = game:GetService("Players")
  7. local Player = Players.LocalPlayer
  8. local Mouse = Player:GetMouse()
  9. local InterfaceItems = {}
  10. local MobileHolding = false
  11. local CursorPosition
  12.  
  13. UserInputService.TouchTap:Connect(function(TouchPositions, GameProcessed)
  14.     if TouchPositions[1] and typeof(TouchPositions[1]) == "Vector2" then
  15.         CursorPosition = TouchPositions[1]
  16.     end
  17. end)
  18.  
  19. UserInputService.TouchMoved:Connect(function(Touch, GameProcessed)
  20.     if Touch and typeof(Touch.Position) == "Vector3" then
  21.         CursorPosition = Vector2.new(Touch.Position.X, Touch.Position.Y)
  22.     end
  23. end)
  24.  
  25. UserInputService.TouchEnded:Connect(function(Touch, GameProcessed)
  26.     if Touch and typeof(Touch.Position) == "Vector3" then
  27.         CursorPosition = Vector2.new(math.huge, math.huge)
  28.     end
  29. end)
  30.  
  31. UserInputService.InputEnded:Connect(function(Input, GameProcessed)
  32.     if Input.UserInputType == Enum.UserInputType.Touch then
  33.         task.wait()
  34.         CursorPosition = Vector2.new(math.huge, math.huge)
  35.     end
  36. end)
  37.  
  38. function CursorInObject(Object)
  39.     if not CursorPosition then return false end
  40.     local Objects = Player.PlayerGui:GetGuiObjectsAtPosition(CursorPosition.X, CursorPosition.Y)
  41.    
  42.     if table.find(Objects, Object) then
  43.         return true
  44.     end
  45.    
  46.     return false
  47. end
  48.  
  49. RunService.Stepped:Connect(function()
  50.     if UserInputService.KeyboardEnabled then
  51.         CursorPosition = Vector2.new(Mouse.X, Mouse.Y)
  52.     end
  53.    
  54.     for Object, ObjectInfo in pairs(InterfaceItems) do
  55.         if typeof(Object) == "number" or typeof(ObjectInfo) ~= "table" or not ObjectInfo.Active or ObjectInfo.CursorPosition == CursorPosition then continue end
  56.         ObjectInfo.CursorPosition = CursorPosition
  57.         local InsetCursorPosition
  58.        
  59.         if ObjectInfo.IgnoreInset then
  60.             InsetCursorPosition = CursorPosition + GuiService:GetGuiInset()
  61.         else
  62.             InsetCursorPosition = CursorPosition
  63.         end
  64.        
  65.         if CursorInObject(Object) then
  66.             if ObjectInfo.CursorWasIn then
  67.                 Object.MoveEvent:Fire(InsetCursorPosition)
  68.                 continue
  69.             end
  70.            
  71.             ObjectInfo.CursorWasIn = true
  72.             Object.EnterEvent:Fire(InsetCursorPosition)
  73.         else
  74.             if not ObjectInfo.CursorWasIn then continue end
  75.             ObjectInfo.CursorWasIn = false
  76.             Object.LeaveEvent:Fire(InsetCursorPosition)
  77.         end
  78.     end
  79. end)
  80.  
  81. local module = {}
  82.  
  83. module.Register = function(Object, Active)
  84.     table.insert(InterfaceItems, Object)
  85.    
  86.     local ObjectInfo = {}
  87.    
  88.     local EnterEvent = Instance.new("BindableEvent", Object)
  89.     local LeaveEvent = Instance.new("BindableEvent", Object)
  90.     local MoveEvent = Instance.new("BindableEvent", Object)
  91.  
  92.     EnterEvent.Name = "EnterEvent"
  93.     LeaveEvent.Name = "LeaveEvent"
  94.     MoveEvent.Name = "MoveEvent"
  95.     ObjectInfo.WasIn = false
  96.     ObjectInfo.CursorPosition = CursorPosition or Vector2.new(math.huge, math.huge)
  97.     ObjectInfo.Active = Active
  98.     ObjectInfo.IgnoreInset = Object:FindFirstAncestorWhichIsA("ScreenGui").IgnoreGuiInset
  99.     InterfaceItems[Object] = ObjectInfo
  100.    
  101.     Object.Destroying:Connect(function()
  102.         InterfaceItems[Object] = nil
  103.     end)
  104.    
  105. end
  106.  
  107. module.Unregister = function(Object)
  108.     if not table.find(InterfaceItems, Object) then return end
  109.     Object.EnterEvent:Destroy()
  110.     Object.LeaveEvent:Destroy()
  111.     InterfaceItems[Object] = nil
  112. end
  113.  
  114. module.Active = function(Object, Active)
  115.     if not table.find(InterfaceItems, Object) then return end
  116.     InterfaceItems[Object].Active = Active
  117. end
  118.  
  119. return module
  120.  
Advertisement
Add Comment
Please, Sign In to add comment