GUI not positioning correctly

Hello! I am trying to make a GUI that becomes visible and positions itself to the mouse, however when I do this it instead goes way higher then the mouse. Does anyone know how to solve this? Here is my code and what happens when I test it.

Script
local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local gui = script.MouseHoverGui
local healthbar = gui.MainFrame.CurrentHealth
local healthtext = healthbar.Parent.Title
local hightlight = script.Highlight

game:GetService("RunService").RenderStepped:Connect(function()
	if mouse.Target then
		if mouse.Target.Parent.Parent == workspace.Mobs then
			local humanoid = mouse.Target.Parent.Humanoid
			local config = mouse.Target.Parent.Config
			local name = config.MobName
			local percent = humanoid.Health / humanoid.MaxHealth
			local X = mouse.X
			local Y = mouse.Y
			hightlight.Adornee = mouse.Target.Parent
			gui.MainFrame.ObjectText.Text = name.Value
			healthtext.Text = "" .. humanoid.Health .. "/" .. humanoid.MaxHealth
			healthbar.Size = UDim2.new(math.max(percent, 0), 0, 1, 0)
			gui.MainFrame.Position = UDim2.new(0,X,0,Y)
			gui.Enabled = true
		else
			hightlight.Adornee = nil
			gui.Enabled = false
			gui.MainFrame.Position = UDim2.new(0, 0, 0, 0)
			gui.MainFrame.ObjectText.Text = ""
		end
	end
end)
Result

image

5 Likes

Its due to anchor point. Move the anchor point to 0.5,0.5

2 Likes

Doesnt fix it, anchor point was 0, 0 before anyways.

2 Likes

then manually add an offset while moving of Udim2.fromOffset(Frame.AbsoluteSize.X/2,Frame.AbsoluteSize.Y/2)

1 Like

Does this still make it size properly on all devices?

Doesn’t even work, might have put it in wrong but this is the error I get.

fromOffset is not a valid member of UDim2

Script with it added:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local gui = script.MouseHoverGui
local healthbar = gui.MainFrame.CurrentHealth
local healthtext = healthbar.Parent.Title
local hightlight = script.Highlight

game:GetService("RunService").RenderStepped:Connect(function()
	if mouse.Target then
		if mouse.Target.Parent.Parent == workspace.Mobs then
			local humanoid = mouse.Target.Parent.Humanoid
			local config = mouse.Target.Parent.Config
			local name = config.MobName
			local percent = humanoid.Health / humanoid.MaxHealth
			local X = mouse.X
			local Y = mouse.Y
			local Udim2 = UDim2.new(math.max(percent, 0), 0, 1, 0)
			Udim2.fromOffset(gui.MainFrame.AbsoluteSize.X/2,gui.MainFrame.AbsoluteSize.Y/2)
			hightlight.Adornee = mouse.Target.Parent
			gui.MainFrame.ObjectText.Text = name.Value
			healthtext.Text = "" .. humanoid.Health .. "/" .. humanoid.MaxHealth
			healthbar.Size = UDim2.new(math.max(percent, 0), 0, 1, 0)
			gui.MainFrame.Position = UDim2.new(0,X,0,Y)
			gui.Enabled = true
		else
			hightlight.Adornee = nil
			gui.Enabled = false
			gui.MainFrame.Position = UDim2.new(0, 0, 0, 0)
			gui.MainFrame.ObjectText.Text = ""
		end
	end
end)
local Udim2 = UDim2.new(math.max(percent, 0), 0, 1, 0) + UDim2.fromOffset(gui.MainFrame.AbsoluteSize.X/2,gui.MainFrame.AbsoluteSize.Y/2)

Thats not even how you add offset…

Its worse now, its still higher up but also way to far to the right.

You’re trying to make a “Tooltip” - esque system. Make sure you’re modifying the correct frame, the anchor point is correct, and where you’re positioning it. Try enabling it, then positioning. One thing that’s interesting is that your GUI is parented to the script. Try the reverse.

Can you please hightlight the line wher the Udim2 variable is being used?

Line 36: gui.MainFrame.Position = UDim2.new(0,X,0,Y) + UDim2.fromOffset(gui.MainFrame.AbsoluteSize.X/2,gui.MainFrame.AbsoluteSize.Y/2)

The other UDim 2 before line 36 is to make the health bar go down, not what positions the entire UI.

This is not very good, and it’s not good practice to name something “Udim2” when there is already something named UDim2. Also, I see you define “Udim2” but you never use it. I’m not understanding that.

More just a GUI that follows your mouse when you hover over certain enemies, but its kind of hard to position it next to the mouse properly if it wont even center properly. The anchor point is correct and the position in the script is telling it to go precisely in the middle. I don’t think putting the script as a child of the GUI will make any difference?

gui.MainFrame.Position = UDim2.new(0,X+gui.MainFrame.AbsoluteSize.X/2,0,Y+gui.MainFrame.AbsoluteSize.Y/2)

?

Yes, thats the line in the script ye.

Yes and thats the fix test it out.

Im not using a variable for it? I am just creating a new UDim2 value when setting its position property.

Same thing, slightly higher and a lot to the right.

I’m trying to explain to you that it doesn’t do anything, the “Udim2” serves no purpose, I think you don’t quite understand how UDim2 works.

Could you show us your explorer? I think that may be the problem, this is really a simple script.

1 Like