Converting Vector2 to UDim2 has some weird bugs

Hey, I’m trying to make a thing where a frame will follow the mouse position wherever it goes

(Made using Scratch lol)

This is my code

And this is my result
image

If you have any solutions, please share! Thanks in advance =]

What exactly is the issue? Is it just that its not exactly on the mouse?

Yeah, it’s not exactly on the mouse and I’m not sure why :frowning:

If the offset is constant, just move it up a bit by adding a Vector2.new()

First of all, you can use WorldToScreenPoint. Nvm, I didn’t read everything. Make sure you’re ScreenGui is maxed out at 1,0,1,0 and that IgnoreGuiInset is enabled.

Mainly just check to see if you have IgnoreGuiInset enabled.

Try this.

local mouse = game.Players.LocalPlayer:getMouse()

mouse.Move:connect(function()
	script.Parent.Parent.Hint.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
end)

that’s exactly what I did tho…

1 Like

local Frame = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Move:Connect(function()
	Frame.Position = UDim2.fromOffset(mouse.X, mouse.Y)
end)

The code worked just fine for me. It may just be something to do with the frame that you are using

Why do you have to use Offset tho?

fromOffset is the same as just doing a UDim2.new(0,x,0,y). Both work the same way
I just like using fromOffset since it’s more convenient and takes less time to type sometimes.

try changing the AnchorPoint value of your frame

if you want it in scale, you can divide it with the camera viewport size, it does not do any visual difference and makes the code longer

you can change line 6 to be local mousePositionVector = Vector2.new(mouse.X, mouse.Y) if the gui is still offsetted on the Y part

local mouse = game.Players.LocalPlayer:GetMouse()

local frame = script.Parent

mouse.Move:Connect(function()
	local mousePositionVector = game.UserInputService:GetMouseLocation()
	local cameraSize = workspace.CurrentCamera.ViewportSize
	
	frame.Position = UDim2.fromScale(mousePositionVector.X / cameraSize.X, mousePositionVector.Y / cameraSize.Y)
end)

Set the screengui to have IgnoreGuiInset as true, or subtract 36 from the mouse Y position.

1 Like

it’s currently at 0.5,0.5 right now

okay, I got the thing working

local mouse = game.Players.LocalPlayer:GetMouse()

local frame = script.Parent

mouse.Move:Connect(function()
	--local mousePositionVector = game.UserInputService:GetMouseLocation()

	--frame.Position = UDim2.new(0, mousePositionVector.X, 0, mousePositionVector.Y)
	local newMouseX = mouse.X + 4
	local newMouseY = mouse.Y + 36

	frame.Position = UDim2.new(0, mouse.X, 0, newMouseY)
end)

HOWEVER. However, when I change it to this script, it doesn’t show up anymore, I really don’t know how to convert Vector2 to UDim2

local mouse = game.Players.LocalPlayer:GetMouse()

local frame = script.Parent

mouse.Move:Connect(function()

  --local mousePositionVector = game.UserInputService:GetMouseLocation()
  
  --frame.Position = UDim2.new(0, mousePositionVector.X, 0, mousePositionVector.Y)
  
  local newMouseX = mouse.X + 4
  
  local newMouseY = mouse.Y + 36
  
  frame.Position = UDim2.fromScale(mouse.X, newMouseY)

end)

mouse.x and mouse.y are not scale based
they are based off the offset of the screen

so using
.fromScale() wont work
it needs to be .fromOffset()

and if you are worried that it wont look right on different screens that doesnt matter since the mouse position is different on different screens so it will always line up with the mouse on any screen

OH REALLY? I never knew, thanks!