Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with mouse please?

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!

2 answers

✓ Accepted answer

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.

2 comments

0

Well if I want to expand that ImageLabel but keep it centered so that when it expands it doesn't go in one direction it goes in all direction is there anyway I'd be able to tween the position like that?

0

Never mind I made it easier I just subtracted a 2 Dimensional value from it's original position I did this already and have made it already thank you for your help!

Line 8 should be: il.Position = UDim2.new(mouse.X, 0, mouse.Y, 0)

1 comment

0

Those should be in the offset, not scale.

This discussion is preserved as it was. New answers, comments, and votes are closed.