Alright, so I am trying to get the mouse's position which is not a Vector3
or a CFrame
but rather it's a UDim2
so in other words I want it in 2 dimensions. I want to make it to where when you left click with your mouse a ScreenGui
gets moved to your PlayerGui
and I have an ImageLabel
inside of the ScreenGui
and the ImageLabel's position goes to the spot that the mouse clicks. That's basically what I want to get the 2Dimensional coordinates of the mouse when it clicks. This is what I got so far....
local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.Button1Down:connect(function() local sg = Instance.new([[ScreenGui]],player.PlayerGui) local il = Instance.new([[ImageLabel]],sg) il.BackgroundTransparency = 1 il.Position = mouse.CoordinateFrame Hit --This won't work! It's not a correct member of mouse. il.Size = UDim2.new(0,0,0,0) il:TweenSizeAndPosition(UDim2.new(1,0,1,0), il.Position, [[InOut]], [[Quad]], .3) end)
I hope someone can help me out and thank you for reading the whole question!
You can get the position of the mouse using the properties, X and Y, of the mouse.
local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.Button1Down:connect(function() local sg = Instance.new([[ScreenGui]],player.PlayerGui) local il = Instance.new([[ImageLabel]],sg) il.BackgroundTransparency = 1 il.Position = UDim2.new(0, mouse.X, 0, mouse.Y) -- Make sure they're in the offset, not scale. il.Size = UDim2.new(0,0,0,0) il:TweenSizeAndPosition(UDim2.new(1,0,1,0), il.Position, [[InOut]], [[Quad]], .3) end)
EDIT: If you want to center it, you'll have to get the size, in offset, of the ImageLabel in question, divide it by 2, and do some math with the position. Since we can read these properties from a script, you can set it up using the same script:
local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.Button1Down:connect(function() local sg = Instance.new([[ScreenGui]],player.PlayerGui) local il = Instance.new([[ImageLabel]],sg) il.BackgroundTransparency = 1 il.Position = UDim2.new(0, (mouse.X - (il.Size.X.Offset/2)), 0, (mouse.Y - (il.Size.Y.Offset/2)) il.Size = UDim2.new(0,0,0,0) il:TweenSizeAndPosition(UDim2.new(1,0,1,0), il.Position, [[InOut]], [[Quad]], .3) end)
After typing this, though, I just saw that you're using scale. I found something about this earlier, I'll edit my answer with a solution to that, as well.
EDIT 2:
This should help you with centering it, I think.
Line 8 should be: il.Position = UDim2.new(mouse.X, 0, mouse.Y, 0)