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