Help with Mouse Position

I have this code:

local mouse = game.Players.LocalPlayer:GetMouse()
local posX = mouse.x
local posY = mouse.y
while wait() do
script.Parent.mouseframe.Position = UDim2.new(posX,0,posY,0)
end

I want the mouseframe to be at the position of the mouse but it doesn’t seem to work.

2 Likes

I’m pretty sure it’s due to the fact that you’re feeding the Scale values of that UDim2 numbers that are much higher than 1 and 0.
Try moving those to the Offset positions.
UDim2.new(0, posX, 0,posY)
Also you might want to add those mouse positions to the loop so they’re getting updated too

2 Likes

Try using this:

local mouse = game.Players.LocalPlayer:GetMouse()
local posX = mouse.x
local posY = mouse.y

while true do
    script.Parent.mouseframe.Position = UDim2.new(0,posX,0,posY)
    game:GetService("RunService").Heartbeat:Wait()
end

Edit: Within base of what people said, i have fixed it.

2 Likes

He’s not wanting the hit position, but the position relative to the screen he has that part correct.
If he prefers to use the scale property how ever then he can translate the position to a scaled position

as show here he can just take the Vector2.new(mouse.x, mouse.y) and divide it by the Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)
and it will give you the position as a scale

3 Likes

It needs to be the offset of the GUI object, not the scale

script.Parent.mouseframe.Position = UDim2.new(0,posX,0,posY)
2 Likes

I just noticed i learned something new, which is that the player’s mouse has the vector2/udim2 properties, set based on the position of the mouse within the screen/ui interaction. (Badly explained but ok.)

1 Like

Yessir, I happen to work with ui a lot, lol. But congrats on learning something new, that’s the overall goal here on the dev forums; we’re all learning and improving based on interactions with eachother.

2 Likes

This is wrong, because you are just setting the variable once, it will not update through the loop. Try this code instead:

local mouse = game.Players.LocalPlayer:GetMouse()
while wait() do
    script.Parent.mouseframe.Position = UDim2.new(mouse.X.Scale,0,mouse.Y.Scale,0)
end
2 Likes