How to find the cursor position inside a gui object

  1. What do you want to achieve? I am trying to move a GUI object inside a GUI object like this image below me
    image

  2. What is the issue? The shadow won’t move to the correct position

  3. What solutions have you tried so far? I tried using the regular technique but that didn’t work because the GUI object is parented inside another gui object

This is the code I currently have

local ts = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
newclone = nil
onhoverclr = Color3.fromRGB(script.Parent.BackgroundColor3.R - 10,script.Parent.BackgroundColor3.G - 10,script.Parent.BackgroundColor3.B - 10)
defaultclr = script.Parent.BackgroundColor3
mouselocation = nil
function NewBubble()
	newclone = script.Parent.Parent.Resources.Bubble:Clone()
	newclone.Parent = script.Parent
	mouselocation = UserInputService:GetMouseLocation()
	newclone.Visible = true
	newclone.Position = mouselocation
	newclone:TweenSize(UDim2.new(0,500,0,500), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 1, true)
	for i=0.4,1,0.4 do
		newclone.BackgroundTransparency = i
		wait()
	end
	newclone:Destroy()
end

script.Parent.MouseButton1Click:Connect(function()
	NewBubble()
end)

script.Parent.MouseEnter:Connect(function()
	ts:Create(script.Parent, TweenInfo.new(0.5), {BackgroundColor3 = onhoverclr}):Play()
end)

script.Parent.MouseLeave:Connect(function()
	ts:Create(script.Parent, TweenInfo.new(0.5), {BackgroundColor3 = defaultclr}):Play()
end)

Please do tell me if there is anything else that I should add, thanks!

1 Like

You have to get the mouse’s position relative to the text buttons position. This can be done like this:

local mouseLocation = UserInputService:GetMouseLocation() - script.Parent.AbsolutePosition

Then you can set the bubbles anchor point to 0.5,0.5 so its position is its middle and tween its size.

Also if the parent ScreenGui has IgnoreGuiInset you will have to add an additional 36 to the y axis to account for this.

1 Like

You need to get the mouses position, which can be done with UserInputService:GetMouseLocation()