Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I get a GUI within an imagelabel to change to the position of my mouse?

I have a script

mouse = game.Players.LocalPlayer:GetMouse()
mouseHover = script.Parent.MouseEnter:connect(function()
	script.Parent.Parent.Description.Visible = true
	 mouseMoved = script.Parent.MouseMoved:connect(function()
		script.Parent.Parent.Description.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
		print(mouse.X, mouse.Y)
		print(script.Parent.Parent.Description.Position)
	end)
end)

script.Parent.MouseLeave:connect(function()
	mouseMoved:disconnect()
	script.Parent.Parent.Description.Position = UDim2.new(0, 0, 0, 0)
	script.Parent.Parent.Description.Visible = false
	print(mouse.X, mouse.Y)
end)

And when the image that is moving is in a space that allows it to utilize the entire screen, it works fine. However, when I put it into a smaller area, things are positioned differently. {0, 0, 0, 0}, for example, is not in the top left of the screen, but in the top left of my GUI. Due to this, my mouse position is within the entire screen, whereas my description label tries to find that place within a GUI to move to. How can I fix this? Thanks.

1 answer

✓ Accepted answer

Basically, get the position of the mouse relative to the parent object's. This can be done with just a bit of subtraction:

script.Parent.Parent.Description.Position = UDim2.new(0, mouse.X - script.Parent.Parent.AbsolutePosition.X, 0, mouse.Y-script.Parent.Parent.AbsolutePosition.Y)

should do the trick.

This discussion is preserved as it was. New answers, comments, and votes are closed.