Skip to content

Instantly share code, notes, and snippets.

@Shilo
Last active September 16, 2022 21:58
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/07af4cc28432b066d2aae3a166cbb496 to your computer and use it in GitHub Desktop.
Save Shilo/07af4cc28432b066d2aae3a166cbb496 to your computer and use it in GitHub Desktop.
Flexible gui tooltip system for Roblox Studio.
1. Add Tooltip.lua to ReplicatedStorage/Tooltip (ModuleScript)
2. Create a Tooltip gui with TextLabel.
3. Create a LocalScript inside the Tooltip gui and paste Example.lua code.
4. Add "Tooltip" Attribute (String) to any Gui objects that you want to have a tooltip.
5. See copyable example: https://www.roblox.com/games/10917149782/GuiTooltipTest
local Tooltip = require(game.ReplicatedStorage.Tooltip)
local tooltipGui = script.Parent
local tooltipTextLabel = tooltipGui.Text
local offset = Vector2.new(16, 0)
local maxSize = Vector2.new(200, math.huge)
local tooltip = Tooltip.new(tooltipGui, tooltipTextLabel, offset, maxSize)
tooltip:AutoShow(script.Parent.Parent)
local Self = {}
Self.__index = Self
local RunService = game:GetService("RunService")
local TextService = game:GetService("TextService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local updateSignal = nil
function Self:GetTooltipPosition()
return UDim2.new(0, mouse.X - self.Tooltip.Parent.AbsolutePosition.X + self.Offset.X, 0, mouse.Y - self.Tooltip.Parent.AbsolutePosition.Y + self.Offset.Y)
end
function Self:GetTooltipSize()
local TextBounds = TextService:GetTextSize(self.TextLabel.Text, self.TextLabel.TextSize, self.TextLabel.Font, self.maxSize)
local tooltipSizeOffset = self.Tooltip.AbsoluteSize - self.TextLabel.AbsoluteSize
local x = TextBounds.X + tooltipSizeOffset.X
if self.maxWidth then x = math.min(x, self.maxWidth) end
return UDim2.fromOffset(x, TextBounds.Y + tooltipSizeOffset.Y)
end
function Self:OnUpdate()
self.Tooltip.Position = self:GetTooltipPosition()
end
function Self.new(tooltip, textLabel, offset, maxSize)
local self = {}
self.Tooltip = tooltip
self.TextLabel = textLabel
self.Offset = offset or Vector2.zero
self.maxSize = maxSize
self.TextLabel.TextWrapped = true
self.Tooltip.Visible = false
return setmetatable(self, Self)
end
function Self:Show(text)
self.TextLabel.Text = text or ""
self.Tooltip.Size = self:GetTooltipSize()
self:OnUpdate()
self.Tooltip.Visible = true
if not updateSignal then
updateSignal = RunService.Heartbeat:Connect(function()
self:OnUpdate()
end)
end
end
function Self:Hide()
if updateSignal then
updateSignal:Disconnect()
updateSignal = nil
end
self.Tooltip.Visible = false
end
function Self:AutoShow(guiParent)
for _, descendant in guiParent:GetDescendants() do
local tooltip = descendant:GetAttribute("Tooltip")
if tooltip then
descendant.MouseEnter:Connect(function()
self:Show(tooltip)
end)
descendant.MouseLeave:Connect(function()
self:Hide()
end)
end
end
end
return Self
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment