How can I show UI by the mouse?

Greetings! I want to show UI by the mouse just like in Tower Defense Simulator:


How can I do this and have the UI move when the cursor moves?

Thanks in advance,

Sincerely, Will

1 Like

You can implement that by watching this video

ROBLOX Scripting - Gui following mouse when hovering over a part

I am not looking for hovering over a part, I am looking for UI around the cursor even if you move it.

I would make the UI always follow the mouse, and you could make it visible once you need it.

How would I make the UI follow the cursor?

I used this in one of my games. This activates when the mouse touches UI:

local button1 = script.Parent.buy1
local image1 = script.Parent.item1
local description = script.Parent.Parent.Description
local screenGui = script:FindFirstAncestorOfClass("ScreenGui")
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local mouse = player:GetMouse()

mouse.Move:Connect(function()
	local xDimension = mouse.X
	local yDimension = mouse.Y
	local xResolution = screenGui.AbsoluteSize.X
	local yResolution = screenGui.AbsoluteSize.Y
	local framePosXStart = math.round(xResolution * description.Position.X.Scale) + description.Position.X.Offset
	local framePosXEnd = framePosXStart + math.round(xResolution * description.Size.X.Scale) + description.Size.X.Offset
	local framePosXMid = math.round((framePosXEnd - framePosXStart)/2)
	local framePosYStart = math.round(yResolution * description.Position.Y.Scale) + description.Position.Y.Offset
	local framePosYEnd = framePosYStart + math.round(xResolution * description.Size.Y.Scale) + description.Size.Y.Offset
	local framePosYMid = math.round((framePosYEnd - framePosYStart)/2)
	description.Position = UDim2.new(0, xDimension-framePosXMid, 0, yDimension-framePosYMid)
end)

button1.MouseEnter:Connect(function()
	script.Parent.Parent.Description.Visible = true
end)

button1.MouseLeave:Connect(function()
	script.Parent.Parent.Description.Visible = false
end)

image1.MouseEnter:Connect(function()
	script.Parent.Parent.Description.Visible = true
end)

image1.MouseLeave:Connect(function()
	script.Parent.Parent.Description.Visible = false
end)

What this does is it changes the position of the UI whenever the mouse touches another UI and makes the description of the item you are trying to buy visible.

1 Like
local UserInputService = game:GetService("UserInputService")

local frame = -- path to frame
local offset = 10

while true do
	local mouse = UserInputService:GetMouseLocation()
	frame.Position = UDim2.fromOffset(mouse.X + offset, mouse.Y + offset)
	
	task.wait()
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.