GUI dragging is offset

script.Parent.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		held = true
		local UserInputService = game:GetService("UserInputService")
		local lastMousePosition = UserInputService:GetMouseLocation()
		local mapPosition = script.Parent.Position
		local mapPositionAsVector2 = Vector2.new(mapPosition .X.Offset, mapPosition .Y.Offset)
		-- not using while loops.
		UserInputService.InputBegan:Connect(function(inputObject, gameProcessed)
			print("yep")
			if (gameProcessed or not held) then return end -- if the input is processed by the Roblox UI, or if the button is not held.
			print("yep2")
			if (inputObject.UserInputType ~= Enum.UserInputType.MouseButton1) then
				return -- we only want UserInputType.MouseButton1. anything else is discarded.
			end
			print("yep3")
			-- either GetMouseLocation or inputObject.Position
			local mapOffsetDelta = UserInputService:GetMouseLocation() - mapPositionAsVector2
			local newMapPosition = mapPositionAsVector2 + mapOffsetDelta

			script.Parent.Position = UDim2.new(0, newMapPosition.X, 0,newMapPosition.Y)
		end)
	end
end)

Something like this? If so, nothing prints now.

No. Move the UserInputService.InputBegan function outside, and replace InputBegan to InputChanged (of the UIS one).

-- keep this function outside the script.Parent.InputChanged function. this is meant
-- to replace that function.
UserInputService.InputChanged:Connect(function(inputObject, gameProcessed)
	print("yep")
	if (gameProcessed or not held) then return end -- if the input is processed by the Roblox UI, or if the button is not held.
	print("yep2")
	if (inputObject.UserInputType ~= Enum.UserInputType.MouseButton1) then
		return -- we only want UserInputType.MouseButton1. anything else is discarded.
	end
	print("yep3")
	-- either GetMouseLocation or inputObject.Position
	local mapOffsetDelta = UserInputService:GetMouseLocation() - mapPositionAsVector2
	local newMapPosition = mapPositionAsVector2 + mapOffsetDelta

	script.Parent.Position = UDim2.new(0, newMapPosition.X, 0,newMapPosition.Y)
end)

Right, got it, it hit me right after I sent that that it’s probably not what you meant lol but it’s having the same problem where it can’t get past the first check, it won’t print “yep2”, I tried removing that check it also can’t seem to see that the input is mouse1button because “yep3” doesn’t print either.

May I see your complete code with the edits? I’ll check if there’s something else causing the issue.

Here’s the parts regarding your work;

script.Parent.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		held = false
	end
end)

local UserInputService = game:GetService("UserInputService")
local lastMousePosition = UserInputService:GetMouseLocation()
local mapPosition = script.Parent.Position
local mapPositionAsVector2 = Vector2.new(mapPosition .X.Offset, mapPosition .Y.Offset)
-- keep this function outside the script.Parent.InputChanged function. this is meant
-- to replace that function.
UserInputService.InputChanged:Connect(function(inputObject, gameProcessed)
	print("yep")
	if (gameProcessed or not held) then return end -- if the input is processed by the Roblox UI, or if the button is not held.
	print("yep2")
	if (inputObject.UserInputType ~= Enum.UserInputType.MouseButton1) then
		return -- we only want UserInputType.MouseButton1. anything else is discarded.
	end
	print("yep3")
	-- either GetMouseLocation or inputObject.Position
	local mapOffsetDelta = UserInputService:GetMouseLocation() - mapPositionAsVector2
	local newMapPosition = mapPositionAsVector2 + mapOffsetDelta

	script.Parent.Position = UDim2.new(0, newMapPosition.X, 0,newMapPosition.Y)
end)

and the full script;

script.Parent.MouseWheelForward:Connect(function()
	script.Parent.Size = script.Parent.Size + UDim2.new(0,50,0,50)
end)

script.Parent.MouseWheelBackward:Connect(function()
	script.Parent.Size = script.Parent.Size - UDim2.new(0,50,0,50)
end)


local held = false
local mouse = game.Players.LocalPlayer:GetMouse()

--local function movemap()
--	while true do
--		wait(.01)
--		if held == true then
--			script.Parent.Position = UDim2.new(0,mouse.X,0,mouse.Y) - UDim2.new(0,script.Parent.Parent.AbsolutePosition.X,0,script.Parent.Parent.AbsolutePosition.Y) + UDim2.new(0,mouse.X,0,mouse.Y) - UDim2.new(0,script.Parent.AbsolutePosition.X,0,script.Parent.AbsolutePosition.Y)
--			print("updated")
--		end
--	end
--end

--script.Parent.InputChanged:Connect(function(input)
--	if input.UserInputType == Enum.UserInputType.MouseButton1 then
--		held = true
--		movemap()
--	end
--end)

--script.Parent.InputEnded:Connect(function(input)
--	if input.UserInputType == Enum.UserInputType.MouseButton1 then
--		held = false
--	end
--end)

local UserInputService = game:GetService("UserInputService")
local lastMousePosition = UserInputService:GetMouseLocation()
local mapPosition = script.Parent.Position
local mapPositionAsVector2 = Vector2.new(mapPosition .X.Offset, mapPosition .Y.Offset)
-- keep this function outside the script.Parent.InputChanged function. this is meant
-- to replace that function.
UserInputService.InputChanged:Connect(function(inputObject, gameProcessed)
	print("yep")
	if (gameProcessed or not held) then return end -- if the input is processed by the Roblox UI, or if the button is not held.
	print("yep2")
	if (inputObject.UserInputType ~= Enum.UserInputType.MouseButton1) then
		return -- we only want UserInputType.MouseButton1. anything else is discarded.
	end
	print("yep3")
	-- either GetMouseLocation or inputObject.Position
	local mapOffsetDelta = UserInputService:GetMouseLocation() - mapPositionAsVector2
	local newMapPosition = mapPositionAsVector2 + mapOffsetDelta

	script.Parent.Position = UDim2.new(0, newMapPosition.X, 0,newMapPosition.Y)
end)

You’ll have to include your frame’s InputBegan to make sure you only start dragging on the frame, and nowhere else. Then deactivate with UserInputService.InputEnded instead of the frame’s InputEnded to stop dragging the map after releasing your mouse in any part of the screen.

I also fixed an issue in my code where I mistyped something when calculating the delta.

-- this is the bottom half of your code
local UserInputService = game:GetService("UserInputService")

local lastMousePosition = Vector2.zero
local mapPositionAsVector2 = Vector2.zero

script.Parent.InputBegan:Connect(function(input)
	if (input.UserInputType ~= Enum.UserInputType.MouseButton1) then return end
	
	-- we store the current mouse and frame positions in this function.
	local mapPosition = script.Parent.Position
	mapPositionAsVector2 = Vector2.new(mapPosition .X.Offset, mapPosition .Y.Offset)
	
	lastMousePosition = UserInputService:GetMouseLocation()
	held = true
end)

UserInputService.InputChanged:Connect(function(inputObject, gameProcessed)
	if (gameProcessed or not held) then return end -- if the input is processed by the Roblox UI, or if the button is not held.
	
	if (inputObject.UserInputType ~= Enum.UserInputType.MouseButton1) then
		return -- we only want UserInputType.MouseButton1. anything else is discarded.
	end
	
	-- either GetMouseLocation or inputObject.Position
	local mapOffsetDelta = UserInputService:GetMouseLocation() - lastMousePosition
	local newMapPosition = mapPositionAsVector2 + mapOffsetDelta

	script.Parent.Position = UDim2.new(0, newMapPosition.X, 0,newMapPosition.Y)
end)

UserInputService.InputEnded:Connect(function(inputObject, gameProcessed)
	if (gameProcessed) then return end
	if (inputObject.UserInputType ~= Enum.UserInputType.MouseButton1) then return end
	held = false
end)

Let me know if there’s still a problem.

Copy and pasted with some print checks and it is passing the second check however it isn’t passing the mouse1down check.;

UserInputService.InputChanged:Connect(function(inputObject, gameProcessed)
	print("yep")
	if (gameProcessed or not held) then return end -- if the input is processed by the Roblox UI, or if the button is not held.
print("yep2")
	if (inputObject.UserInputType ~= Enum.UserInputType.MouseButton1) then
		return -- we only want UserInputType.MouseButton1. anything else is discarded.
	end
	print("yep3")
	-- either GetMouseLocation or inputObject.Position

It only prints up to yep2, I tried to do an == and put the end at the end but that didn’t work either. Printing the userinputtype from the inputobj says that it doesn’t register mousebutton1 downs at all, only mouse movement and doesn’t even register that when hovering over the GUI.

I missed that, my bad. In the part where the input type is being checked, change Enum.UserInputType.MouseButton1 to Enum.UserInputType.MouseMovement.

This is actually working more however it stops when hovering over the GUI;
https://gyazo.com/f7840c0f9aae6c41f183824d85ef26cb

In the InputChanged function, try removing the entire if (gameProcessed) line. I just realised now that it will stop the function because gameProcessed returns true when over a GUI element.

Haha, now we’re back to where we started, it’s working but it’s offset.
radio test - Roblox Studio (gyazo.com)

Keep the held check in the InputChanged function. I should not have said to remove the entire line, my bad.

if (held) then return end

Got it and did it but it didn’t change anything, it’s still starting as soon as the game loads up with no way to stop it and is still offset.

Where did you place the line? May I see again the relevant code again with the edits?

Course, I put it at the top;

UserInputService.InputChanged:Connect(function(inputObject, gameProcessed)
	if (held) then return end
	if (inputObject.UserInputType ~= Enum.UserInputType.MouseMovement) then
		return -- we only want UserInputType.MouseButton1. anything else is discarded.
	end
	-- either GetMouseLocation or inputObject.Position
	local mapOffsetDelta = UserInputService:GetMouseLocation() - lastMousePosition
	local newMapPosition = mapPositionAsVector2 + mapOffsetDelta

	script.Parent.Position = UDim2.new(0, newMapPosition.X, 0,newMapPosition.Y)
end)

Apologies if this took too many replies. Put not behind held in the line I was referring.

if (not held) then return end 

Awesome! It’s completely working now, just need a way to stop dragging, this bottom part should be the part that stops the dragging right? If so, it’s not

UserInputService.InputEnded:Connect(function(inputObject, gameProcessed)
	if (gameProcessed) then return end
	if (inputObject.UserInputType ~= Enum.UserInputType.MouseButton1) then return end
	held = false
end)
1 Like

Sorry! I got it working, I messed with it a little and removed the gameprocess but got it working, thank you for your help!

1 Like

Full finished script for those looking for the solution;

script.Parent.MouseWheelForward:Connect(function()
	script.Parent.Size = script.Parent.Size + UDim2.new(0,50,0,50)
end)

script.Parent.MouseWheelBackward:Connect(function()
	script.Parent.Size = script.Parent.Size - UDim2.new(0,50,0,50)
end)


local held = false
local UserInputService = game:GetService("UserInputService")

local lastMousePosition = Vector2.zero
local mapPositionAsVector2 = Vector2.zero

script.Parent.InputBegan:Connect(function(input)

	if (input.UserInputType ~= Enum.UserInputType.MouseButton1) then return end

	-- we store the current mouse and frame positions in this function.
	local mapPosition = script.Parent.Position
	mapPositionAsVector2 = Vector2.new(mapPosition .X.Offset, mapPosition .Y.Offset)

	lastMousePosition = UserInputService:GetMouseLocation()
	held = true
end)

UserInputService.InputChanged:Connect(function(inputObject, gameProcessed)
	if (not held) then return end
	if (inputObject.UserInputType ~= Enum.UserInputType.MouseMovement) then
		return -- we only want UserInputType.MouseButton1. anything else is discarded.
	end
	-- either GetMouseLocation or inputObject.Position
	local mapOffsetDelta = UserInputService:GetMouseLocation() - lastMousePosition
	local newMapPosition = mapPositionAsVector2 + mapOffsetDelta

	script.Parent.Position = UDim2.new(0, newMapPosition.X, 0,newMapPosition.Y)
end)

UserInputService.InputEnded:Connect(function(inputObject, gameProcessed)
	if (inputObject.UserInputType ~= Enum.UserInputType.MouseButton1) then return end
	held = false
end)