Skip to content

Instantly share code, notes, and snippets.

@Shilo
Last active September 12, 2022 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shilo/0efd24aa52e3061920ff81168126867e to your computer and use it in GitHub Desktop.
Save Shilo/0efd24aa52e3061920ff81168126867e to your computer and use it in GitHub Desktop.
Mouse tooltip for showing name of hovered part. (StarterGui LocalScript in Roblox Studio)
1. Setup StarterGui
- PartTooltip (Frame)
- PartTooltip (LocalScript)
- Whitelist (ObjectValue that has a Value of the whitelist object or folder [Optional])
- Text (TextLabel)
- UICorner [Optional]
2. Create an "Offset" (Vector2) attribute for PartTooltip LocalScript.
3. Create a "Distance" (Number) attribute for PartTooltip LocalScript. (This is max distance to show tooltip)
local RunService = game:GetService("RunService")
local TextService = game:GetService("TextService")
local tooltip = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tooltipSizeOffset = tooltip.AbsoluteSize - tooltip.Text.AbsoluteSize
local function GetWhitelistedMouseTarget()
local target = mouse.Target
if not target then return nil end
local whitelist = script.Whitelist.Value
if whitelist and not target:IsDescendantOf(whitelist) then return nil end
if target then
local maxDistance = script:GetAttribute("Distance")
if maxDistance and maxDistance >= 0 then
local distance = player:DistanceFromCharacter(target.Position)
if distance > maxDistance then
target = nil
end
end
end
return target
end
local function GetTooltipPosition()
local offset = script:GetAttribute("Offset") or Vector2.zero
return UDim2.new(0, mouse.X + offset.X, 0, mouse.Y + offset.Y)
end
local function GetTooltipSize()
return UDim2.new(0, tooltip.Text.TextBounds.X + tooltipSizeOffset.X, 0, tooltip.Text.TextBounds.Y + tooltipSizeOffset.Y)
end
RunService.Heartbeat:Connect(function()
local target = GetWhitelistedMouseTarget()
tooltip.Visible = target
if not target then return end
tooltip.Text.Text = target.Name
tooltip.Position = GetTooltipPosition()
tooltip.Size = GetTooltipSize()
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment