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)
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)
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.
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?