GUI not following mouse movement

I’m making a chest system that requires an image label to lock to the player’s mouse. However, as soon as my mouse makes any movement, it goes wayyyy off screen to a place where you can’t see it anymore. What can I do to fix this?

UIS.InputChanged:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseMovement then
			local Position = UIS:GetMouseLocation()
			local ImagePosition = UDim2.fromScale(Position.X,Position.Y)
			ItemImage.Position = ImagePosition
			wait()
		end
	end)

Full script if needed:

local UIS = game:GetService("UserInputService")
local Hitbox = script.Parent
local ItemImage = Hitbox.Parent.ItemImage
local ScreenSize = game.Players.LocalPlayer.PlayerGui.ScreenGUI.AbsoluteSize
Hitbox.MouseButton1Click:Connect(function()
	local Position = UIS:GetMouseLocation()
	local ImagePosition = UDim2.fromScale(Position.X/ScreenSize.X,Position.Y/ScreenSize.Y)
	ItemImage.Position = ImagePosition
	UIS.InputChanged:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseMovement then
			local Position = UIS:GetMouseLocation()
			local ImagePosition = UDim2.fromScale(Position.X,Position.Y)
			ItemImage.Position = ImagePosition
			wait()
		end
	end)
end)
2 Likes

Set the offset of the position to the mouse position

UIS.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		local Position = UIS:GetMouseLocation()
		local ImagePosition = UDim2.new(0, Position.X, 0, Position.Y)
		ItemImage.Position = ImagePosition
		task.wait()
	end
end)

If you want the image directly in the middle of the cursor, set the anchor point of ItemImage to 0.5,0.5

1 Like


This is what happens when doing that. The mouse disappeared when screenshotting, but the circle is where it was

3 Likes

The issue is that the parent (and ascendants/ancestors) of the image kind of offset the image with their size. I’m currently trying to figure out how to get over this

3 Likes

The hierarchy looks like this if you need it:
image

3 Likes

You could perhaps try parenting the image under something else thats the size of your screen. I don’t know.

3 Likes

Would it work if I just parented to a folder in “Chest” called something like “ClickedSlot”? Chest is parented to ScreenGUI

3 Likes

That would probably work. Only thing I can think of. Sounds very inefficient though.

2 Likes

What if I had an already stored imageLabel that I could just set its image to the slot image when clicked, and make the slot image invisible?

1 Like

Yeah, that’s probably the more optimized option. That would prevent a lot of bugs.

3 Likes

Figured it out!

Hitbox.MouseButton1Click:Connect(function()
	local Position = UIS:GetMouseLocation()
	local ImagePosition = UDim2.fromOffset(Position.X,Position.Y)
	
	SlotImage.Image = ItemImage.Image
	SlotImage.Counter.Text = ItemImage.Counter.Text
	SlotImage.Position = ImagePosition
	ItemImage.Image = ""
	ItemImage.Counter.Text = ""
	
	
	UIS.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		local Position = UIS:GetMouseLocation()
		local ImagePosition = UDim2.fromOffset(Position.X,Position.Y)
		SlotImage.Position = ImagePosition
		task.wait()
	end
end)
end)

Thank you for your help!!!

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.