UDim2.fromOffset positioning is way off from the cursor's location (GUI)

Hello, I am trying to make a textLabel which follows the user’s cursor around. The only problem, is that the label is very offset from the cursor.

Here is my code:

local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()

local event = script.Parent:WaitForChild("GivePrice")
local priceLabel = script.Parent:WaitForChild("PriceLabel")



event.Event:Connect(function(pass, price, button)
	if pass then
		priceLabel.Visible = true
		priceLabel.Text = "$" .. price
		
		while pass do
			priceLabel.Position = UDim2.fromOffset(mouse.X, mouse.Y)
			wait()
		end
		
	end
	
	if not pass then
		priceLabel.Visible = false
		priceLabel.Text = "$"..tostring(nil)
	end
end)

Here is what happens, as you can see there is insane offset.

Could you print the mouse’s X and Y position?
And the textLabel’s AbsolutePosition also?

doing

			print(priceLabel.Position)
			print(mouse.X..", "..mouse.Y)
			print(priceLabel.AbsolutePosition)

got the results (also yes mouseLabel position wasn’t visible but it was the same as mousex and mousey):

Can you send the hierarchy of the priceLabel?
Try parenting the priceLabel to the StarterGui, to see if that helps.

image
Hmm i will try having it just under StarterGUI

Screenshot 2024-08-20 at 11.50.40 AM
Placing it under startergui doesn’t have it move anywhere (cursor above, label below)

Try on the ScreenGui

1 Like

yeah uh i tried this after it didnt work but thanks this did fix the issue.

I have proposed another solution if you don’t want to reparent it to it’s ancestor ScreenGui.

frame = script.Parent
parentFrame = frame.Parent

mouse = game.Players.LocalPlayer:GetMouse()
function a()
    -- also works with non-(0, 0) anchor point
	local difference = parentFrame.AbsolutePosition - Vector2.new(mouse.X, mouse.Y)
	local wanted_pos = Vector2.zero - difference
	local X = wanted_pos.X
	local Y = wanted_pos.Y
	
	frame.Position = UDim2.fromOffset(X, Y)
end

game:GetService("RunService").RenderStepped:Connect(a)

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