Ability to make UI elements stick to the player's cursor more easily

As a Roblox developer, it is currently too hard to make a ScreenGui follow a player’s cursor without additional scripting logic. Yes, we could update position on-the-fly to fix this problem however, this might be intimidating for beginner scripters.

An example of a project that could benefit from the addition of this API member is a stamping feature. A stamping feature is when a player could stamp an image onto another ScreenGui. This feature may be useful for simulators trying to replicate the workforce. An example of the stamping feature in action is this video clip: https://gyazo.com/1406c5f9135318dd09a26d2de92a88c1 (note: UDim2 was used in this code)

If Roblox were to address this issue, it would save development time to make UI elements follow the cursor. It also makes our code easy to read. If our code is short and simple, then it lessens the stress from reading too much code and figuring out its function.

Proposed solution: Add ScreenGui.FollowCursor()

Definition:

ScreenGui:FollowCursor() is a function that allows a ScreenGui and it’s contents to follow the player’s cursor.

Properties:

  • Player (Instance) - defines the client the function will take effect in.
  • Offset X (Integer) - defines the offset of the ScreenGui in the x-axis. If nil, value is automatically 0.
  • Offset Y (Integer) - defines the offset of the ScreenGui in the y-axis. If nil, value is automatically 0.
  • Hover (Bool) - defines if the ScreenGui will hover over the mouse or not. If nil, value is automatically true.

Uses:

  • Simplifies code for functions requiring a ScreenGui to follow the cursor.

Code comparison

Here are some examples where we can use this function to reduce our code:

Code without utilizing FollowCursor():

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local ScreenGui = script.Parent
local mouse = player:GetMouse()

RunService:BindToRenderStep("mouseMove", 1, function()
ScreenGui.Frame.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
end)

Code that utilizes FollowCursor()

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local ScreenGui = script.Parent

while true do
    ScreenGui:FollowCursor(player)
end

or -- ^no offset v5px offset

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local ScreenGui = script.Parent

while true do
    ScreenGui:FollowCursor(player, 5, 5)
end
3 Likes

This is too simple to make a whole new api for. You just have to have the anchor point at 0.5, 0.5 and connect render stepped with the reposition of the UI element(where the mouse is).
This would be the code to reposition the UI where the mouse is:

RS.RenderStepped:Connect(function()
    Frame.Position = Vector2.new(Mouse.X, Mouse.Y)
end)
9 Likes

Isn’t this going to always be the client that has the GuiObject… You shouldn’t, and in many cases can’t, control the GUI from the server.

But all in all this seems like too small of a use case for an API member. If it was something someone could trigger in Studio without needing scripting, then fair enough, but if it requires scripting both with and without this member, then it makes sense to just leave it as it is.

1 Like

Yes I do agree that it is too simple to be made into an API, but this is only a proposed solution. We could add more properties to the code that would make it more efficient and more useful to the developer or completely change the solution. What is important is that we are able to have a gui follow the player’s cursor with ease.

The reason I proposed this API is for simplifying code (similar to how Player.Character works). In the code with the API, 6 lines are only used while 8 are used without the API (without the blank spaces). In addition, beginner coders might feel intimidated by RenderStepped.

Oh, I completely forgot about this. Sorry for the hiccup.

This is just a proposed solution. It could also be a property similar to Frame.Draggable.

1 Like