When I use InputBegan/Ended it trigger when I move my mouse in the UI and I don’t really want that I only want it to trigger when the button is clicked or tapped and when I use MouseButton1Up/Down the button is not triggered if I move my mouse out side of the UI is there another a fix to this or is there another method to fix it?
--// Services //--
local _players = game:GetService("Players")
local _userInputService = game:GetService("UserInputService")
local _workspace = game:GetService("Workspace")
local _replicatedStorage = game:GetService("ReplicatedStorage")
--// Location //--
local camera = _workspace.CurrentCamera
local player = _players.LocalPlayer
local character = player.Character
local playerGui = player.PlayerGui
local AbilityUi = playerGui:WaitForChild("AbilityUi"):WaitForChild("AbilityMargin")
--// Finding Position //--
local function MouseRaycast()
local mousePosition = _userInputService:GetMouseLocation()
local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
local params = RaycastParams.new()
params.FilterDescendantsInstances = {character}
params.FilterType = Enum.RaycastFilterType.Exclude
return workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, params)
end
--// Getting Input //--
local beginPosition
local endPosition
for _,ui in AbilityUi:GetChildren() do
if ui:IsA("ImageButton") then
ui.MouseButton1Down:Connect(function()
beginPosition = MouseRaycast().Position
print("input began", beginPosition)
end)
ui.MouseButton1Up:Connect(function()
endPosition = MouseRaycast().Position
local direction = beginPosition-endPosition
print("input ended", endPosition)
_replicatedStorage.Events.abilityInput:FireServer(player, direction, ui.Name)
end)
end
end
Ok I found the solution if you put an invisible image button it fixes the mouse enter thing for some reason I don’t know how that works but yeah it works
Honestly, the real solution is fixing your code. I can’t find the oiginal link I copied this off a guy long ago after watching MonzterDev videos here is the code I will try and find the link to the community resource.
local Player = game.Players.LocalPlayer or game.Players:GetPropertyChangedSignal("LocalPlayer")
local Mouse = Player:GetMouse()
local CurrentItems = {}
--Private functions
local function IsInFrame(v)
local X = Mouse.X
local Y = Mouse.Y
if X>v.AbsolutePosition.X and Y>v.AbsolutePosition.Y and X<v.AbsolutePosition.X+v.AbsoluteSize.X and Y<v.AbsolutePosition.Y+v.AbsoluteSize.Y then
return true
else
return false
end
end
local function CheckMouseExited(Object)
if not Object.MouseIsInFrame and Object.MouseWasIn then --Mouse was previously over object, fire leave event
Object.MouseWasIn = false
Object.LeaveEvent:Fire()
end
end
local function CheckMouseEntered(Object)
if Object.MouseIsInFrame and not Object.MouseWasIn then
Object.MouseWasIn = true
Object.EnteredEvent:Fire()
end
end
game:GetService("RunService").Heartbeat:Connect(function()
--Check each UI object
--All exit events fire before all enter events for ease of use, so check for mouse exit events here
for _, Object in pairs(CurrentItems) do
Object.MouseIsInFrame = IsInFrame(Object.UIObj)
CheckMouseExited(Object)
end
--Now check if the mouse entered any frames
for _, Object in pairs(CurrentItems) do
CheckMouseEntered(Object)
end
end)
--Public functions
local module = {}
function module.MouseEnterLeaveEvent(UIObj)
if CurrentItems[UIObj] then
return CurrentItems[UIObj].EnteredEvent.Event,CurrentItems[UIObj].LeaveEvent.Event
end
local newObj = {}
newObj.UIObj = UIObj
local EnterEvent = Instance.new("BindableEvent")
local LeaveEvent = Instance.new("BindableEvent")
newObj.EnteredEvent = EnterEvent
newObj.LeaveEvent = LeaveEvent
newObj.MouseWasIn = false
CurrentItems[UIObj] = newObj
UIObj.AncestryChanged:Connect(function()
if not UIObj.Parent then
--Assuming the object has been destroyed as we still dont have a .Destroyed event
--If for some reason you parent your UI elements to nil after calling this, then parent it back again, mouse over will still have been disconnected.
EnterEvent:Destroy()
LeaveEvent:Destroy()
CurrentItems[UIObj] = nil
end
end)
return EnterEvent.Event,LeaveEvent.Event
end
return module