How to make UI object move towards the mouse

I want to make a UI object move towards the mouse. Note, I DO NOT want it to teleport to the mouse. It needs to somewhat trail behind the mouse.

So once again, the object should gradually move towards my mouse. If I move my mouse while it’s moving towards it, it should change course to match the new position.

Ignore my kinda wacky/failed atttempt

while true do
	local MouseLocation = game:GetService("UserInputService"):GetMouseLocation()
	
	local CurrentPosition = script.Parent.Follow.AbsolutePosition
	
	local Distance = (CurrentPosition - MouseLocation).Magnitude
	
	
	local MoveToPosition
	
	if CurrentPosition.X > MouseLocation.X and CurrentPosition.Y > MouseLocation.Y then
		MoveToPosition = UDim2.fromOffset(
			CurrentPosition.X - MouseLocation.X,
			CurrentPosition.Y - MouseLocation.Y
		)
	elseif CurrentPosition.X < MouseLocation.X and CurrentPosition.Y > MouseLocation.Y then
		MoveToPosition = UDim2.fromOffset(
			MouseLocation.X - CurrentPosition.X,
			CurrentPosition.Y - MouseLocation.Y
		)
	elseif CurrentPosition.X > MouseLocation.X and CurrentPosition.Y < MouseLocation.Y then
		MoveToPosition = UDim2.fromOffset(
			CurrentPosition.X - MouseLocation.X,
			MouseLocation.Y - CurrentPosition.Y
		)
	else
		MoveToPosition = UDim2.fromOffset(
			MouseLocation.X - CurrentPosition.X,
			MouseLocation.Y - CurrentPosition.Y
		)
	end
	
	local TweenTo = TweenService:Create(
		script.Parent.Follow,
		TweenInfo.new(0.2),
		{
			Position = MoveToPosition
		}
	)
	TweenTo:Play()
	TweenTo.Completed:Wait()
end

ezgif.com-gif-maker (31)

1 Like
local frame = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local ts = game:GetService("TweenService")

mouse.Move:Connect(function()
	local pos = UDim2.new(-frame.Size.X.Scale/2, mouse.X-frame.Size.X.Offset/2, -frame.Size.Y.Scale/2, mouse.Y-frame.Size.Y.Offset/2)
	local tween = ts:Create(frame, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {Position = pos})
	tween:Play()
	print(frame.Position)
end)

https://gyazo.com/357a7e38e7bfb3684abb4a75fd4f3b19

I think you should add a :Destroy() on it after you’re done with the tween as you’ll create lots of tween every time the event fires.

It gets garbage collected anyway otherwise I would have.

Ah okay, seems like you really are an experienced scripter. No wonder your posts are professional written. I admired that.

1 Like

Can I just use this is a loop? Mouse is deprecated, thus I don’t use it

1 Like

It’s superseded not deprecated, and an indefinite loop is considerably worse.

https://developer.roblox.com/en-us/api-reference/class/Mouse

1 Like

Superseded, either way. It’s not recommended to use

1 Like

Instead of finding the relative offset and computing for each case, why not use the absolute position of the mouse?

1 Like

It remains supported because of its widespread use

From the official documentation.

1 Like

:Remove() is still a supported function, doesn’t mean you should use it

1 Like

https://developer.roblox.com/en-us/api-reference/function/Instance/Remove

That one has actually been deprecated.

1 Like

Yes, but there’s better alternatives. Mouse could get deprecated any day. If there’s better alternatives, then I’ll take them

1 Like

Just move it into a RenderStepped loop if you’re so against using the mouse object then.

1 Like

Wouldn’t Stepped or Heartbeat be better options?

1 Like

It wouldn’t actually changed anything because you’re still using the mouse object for the position even if you do away with the mouse.Move event.

RenderStepped is exclusive to local scripts hence I suggested it be used but the choice is ultimately yours.

1 Like

Another reason why I’d like to use a loop, is I wanna be able to check distance. Idea would be if close enough, the frame would then lock to the mouse

1 Like
frame:GetPropertyChangedSignal("Position"):Connect(function()
	--determine position from mouse
end)

You wouldn’t need a loop for that either.

1 Like

Sorry that this is kinda bump, but for anybody that comes across this post in the future, there’s up to date method how to get Mouse.Move:

local UserInputService = game:GetService("UserInputService")

UserInputService.InputChanged:Connect(function(inputObject)
       if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
              print("Mouse moved to position:",UserInputService:GetMouseLocation())
       end
end)
1 Like