I have a camera dragging script for my game that I use to move around the world. It uses MouseDown like this:
local MoveConnection
Mouse.Button1Down:Connect(function()
MoveConnection = Mouse.Move:Connect(function()
-- Move camera code ommited
end)
end)
Mouse.Button1Up:Connect(function()
if MoveConnection and MoveConnection.Connected then
MoveConnection:Disconnect()
end
end)
For some reason it can get stuck holding the mouse even when the player has let go. At first I got the problem when I put my mouse over a GUI object like a button and then releasing the mouse, so I just added code in the MouseDown function that hid the guis and showed them when the mouse was released. This worked initially and in my playtesting the problem seemed to go away. However, sometimes it gets caught randomly anyway, and I have no idea how to fix it. I think the problem is the home button at the top left, the one that you click to open the leave and reset character menu. I would like to know how to resolve this and ideally not have to hide any GUI objects for it. I’ve been scratching my head over it for a little while.
I’d probably just flag mouse down as true or false during the connections above and farm out the logic of switching it on and off in a step function, some rough (untested code) below:
local RunService = game:GetService("RunService");
local MoveConnection = nil;
local DoMoveFlag = false;
Mouse.Button1Down:Connect(function()
-- it may also be pertinent to use a debounce to prevent swathes of messages spamming this logic
DoMoveFlag = true;
end)
Mouse.Button1Up:Connect(function()
-- it may also be pertinent to use a debounce to prevent swathes of messages spamming this logic
DoMoveFlag = false;
end)
RunService.Heartbeat:Connect(function(step)
if DoMoveFlag == true then
if MoveConnection == nil then
MoveConnection = Mouse.Move:Connect(function()end)
end
else
if MoveConnection ~= nil then
MoveConnection:Disconnect();
MoveConnection = nil;
end
end
end)
I don’t think you understand. Its not the MouseUp or MouseDown functions themselves, it’s the fact that it can get caught when the mouse is released on a GUI element, and not fire the MouseUp or MouseDown event at all.
It works completely fine when the mouse isn’t released over a GUI element
This service will allow you too hook all manner of input changes (i.e. when mouse goes from 3D game screen to 2D GUI screen) using InputBegan, InputChanged and InputEnded. The inputObject supplied as a parameter to these functions will offer you better control over player based input events.