Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 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.
- local UserInputService = game:GetService("UserInputService")
- local GuiService = game:GetService("GuiService")
- local RunService = game:GetService("RunService")
- local Players = game:GetService("Players")
- local Player = Players.LocalPlayer
- local Mouse = Player:GetMouse()
- local InterfaceItems = {}
- local MobileHolding = false
- local CursorPosition
- UserInputService.TouchTap:Connect(function(TouchPositions, GameProcessed)
- if TouchPositions[1] and typeof(TouchPositions[1]) == "Vector2" then
- CursorPosition = TouchPositions[1]
- end
- end)
- UserInputService.TouchMoved:Connect(function(Touch, GameProcessed)
- if Touch and typeof(Touch.Position) == "Vector3" then
- CursorPosition = Vector2.new(Touch.Position.X, Touch.Position.Y)
- end
- end)
- UserInputService.TouchEnded:Connect(function(Touch, GameProcessed)
- if Touch and typeof(Touch.Position) == "Vector3" then
- CursorPosition = Vector2.new(math.huge, math.huge)
- end
- end)
- UserInputService.InputEnded:Connect(function(Input, GameProcessed)
- if Input.UserInputType == Enum.UserInputType.Touch then
- task.wait()
- CursorPosition = Vector2.new(math.huge, math.huge)
- end
- end)
- function CursorInObject(Object)
- if not CursorPosition then return false end
- local Objects = Player.PlayerGui:GetGuiObjectsAtPosition(CursorPosition.X, CursorPosition.Y)
- if table.find(Objects, Object) then
- return true
- end
- return false
- end
- RunService.Stepped:Connect(function()
- if UserInputService.KeyboardEnabled then
- CursorPosition = Vector2.new(Mouse.X, Mouse.Y)
- end
- for Object, ObjectInfo in pairs(InterfaceItems) do
- if typeof(Object) == "number" or typeof(ObjectInfo) ~= "table" or not ObjectInfo.Active or ObjectInfo.CursorPosition == CursorPosition then continue end
- ObjectInfo.CursorPosition = CursorPosition
- local InsetCursorPosition
- if ObjectInfo.IgnoreInset then
- InsetCursorPosition = CursorPosition + GuiService:GetGuiInset()
- else
- InsetCursorPosition = CursorPosition
- end
- if CursorInObject(Object) then
- if ObjectInfo.CursorWasIn then
- Object.MoveEvent:Fire(InsetCursorPosition)
- continue
- end
- ObjectInfo.CursorWasIn = true
- Object.EnterEvent:Fire(InsetCursorPosition)
- else
- if not ObjectInfo.CursorWasIn then continue end
- ObjectInfo.CursorWasIn = false
- Object.LeaveEvent:Fire(InsetCursorPosition)
- end
- end
- end)
- local module = {}
- module.Register = function(Object, Active)
- table.insert(InterfaceItems, Object)
- local ObjectInfo = {}
- local EnterEvent = Instance.new("BindableEvent", Object)
- local LeaveEvent = Instance.new("BindableEvent", Object)
- local MoveEvent = Instance.new("BindableEvent", Object)
- EnterEvent.Name = "EnterEvent"
- LeaveEvent.Name = "LeaveEvent"
- MoveEvent.Name = "MoveEvent"
- ObjectInfo.WasIn = false
- ObjectInfo.CursorPosition = CursorPosition or Vector2.new(math.huge, math.huge)
- ObjectInfo.Active = Active
- ObjectInfo.IgnoreInset = Object:FindFirstAncestorWhichIsA("ScreenGui").IgnoreGuiInset
- InterfaceItems[Object] = ObjectInfo
- Object.Destroying:Connect(function()
- InterfaceItems[Object] = nil
- end)
- end
- module.Unregister = function(Object)
- if not table.find(InterfaceItems, Object) then return end
- Object.EnterEvent:Destroy()
- Object.LeaveEvent:Destroy()
- InterfaceItems[Object] = nil
- end
- module.Active = function(Object, Active)
- if not table.find(InterfaceItems, Object) then return end
- InterfaceItems[Object].Active = Active
- end
- return module
Advertisement
Add Comment
Please, Sign In to add comment