roblox-gui
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRoblox GUI Reference
Roblox GUI参考
GUI Container Types
GUI容器类型
| Container | Parent | Use Case |
|---|---|---|
| | HUDs, menus, overlays — always faces screen |
| | World-space UI on a part surface (signs, screens) |
| | Floats above a part in 3D space (name tags, health bars) |
| 容器 | 父级 | 使用场景 |
|---|---|---|
| | HUD、菜单、覆盖层——始终朝向屏幕 |
| | 零件表面的世界空间UI(标识牌、显示屏) |
| | 悬浮在3D空间零件上方的UI(名称标签、血量条) |
ScreenGui
ScreenGui
lua
-- LocalScript in StarterGui or StarterPlayerScripts
local player = game:GetService("Players").LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "HUD"
screenGui.ResetOnSpawn = false -- keep GUI across respawns
screenGui.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
screenGui.Parent = playerGuilua
-- LocalScript in StarterGui or StarterPlayerScripts
local player = game:GetService("Players").LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "HUD"
screenGui.ResetOnSpawn = false -- keep GUI across respawns
screenGui.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
screenGui.Parent = playerGuiSurfaceGui
SurfaceGui
lua
local surfaceGui = Instance.new("SurfaceGui")
surfaceGui.Face = Enum.NormalId.Front
surfaceGui.SizingMode = Enum.SurfaceGuiSizingMode.PixelsPerStud
surfaceGui.PixelsPerStud = 50
surfaceGui.Parent = workspace.ScreenPart
local label = Instance.new("TextLabel")
label.Size = UDim2.fromScale(1, 1)
label.Text = "Hello World"
label.Parent = surfaceGuilua
local surfaceGui = Instance.new("SurfaceGui")
surfaceGui.Face = Enum.NormalId.Front
surfaceGui.SizingMode = Enum.SurfaceGuiSizingMode.PixelsPerStud
surfaceGui.PixelsPerStud = 50
surfaceGui.Parent = workspace.ScreenPart
local label = Instance.new("TextLabel")
label.Size = UDim2.fromScale(1, 1)
label.Text = "Hello World"
label.Parent = surfaceGuiBillboardGui
BillboardGui
lua
local billboard = Instance.new("BillboardGui")
billboard.Size = UDim2.fromOffset(200, 50)
billboard.StudsOffset = Vector3.new(0, 2.5, 0) -- float above head
billboard.AlwaysOnTop = false
billboard.Parent = character:WaitForChild("Head")
local nameLabel = Instance.new("TextLabel")
nameLabel.Size = UDim2.fromScale(1, 1)
nameLabel.BackgroundTransparency = 1
nameLabel.Text = player.DisplayName
nameLabel.Parent = billboardlua
local billboard = Instance.new("BillboardGui")
billboard.Size = UDim2.fromOffset(200, 50)
billboard.StudsOffset = Vector3.new(0, 2.5, 0) -- float above head
billboard.AlwaysOnTop = false
billboard.Parent = character:WaitForChild("Head")
local nameLabel = Instance.new("TextLabel")
nameLabel.Size = UDim2.fromScale(1, 1)
nameLabel.BackgroundTransparency = 1
nameLabel.Text = player.DisplayName
nameLabel.Parent = billboardUDim2 Sizing and Positioning
UDim2尺寸与定位
UDim2.new(xScale, xOffset, yScale, yOffset)lua
frame.Size = UDim2.new(1, 0, 0, 50) -- full width, 50px tall
frame.Position = UDim2.new(0, 0, 0, 0) -- top-left corner
frame.Size = UDim2.fromScale(0.6, 0.4) -- 60% wide, 40% tall
frame.Position = UDim2.new(0.2, 0, 0.3, 0) -- centered (0.2 = (1-0.6)/2)
UDim2.fromScale(0.5, 0.5) -- scale only
UDim2.fromOffset(300, 150) -- pixels onlyAnchorPoint shifts the element's pivot (0–1 on each axis):
lua
frame.AnchorPoint = Vector2.new(0.5, 0.5) -- pivot at center
frame.Position = UDim2.fromScale(0.5, 0.5) -- truly centered on screenUDim2.new(xScale, xOffset, yScale, yOffset)lua
frame.Size = UDim2.new(1, 0, 0, 50) -- full width, 50px tall
frame.Position = UDim2.new(0, 0, 0, 0) -- top-left corner
frame.Size = UDim2.fromScale(0.6, 0.4) -- 60% wide, 40% tall
frame.Position = UDim2.new(0.2, 0, 0.3, 0) -- centered (0.2 = (1-0.6)/2)
UDim2.fromScale(0.5, 0.5) -- scale only
UDim2.fromOffset(300, 150) -- pixels onlyAnchorPoint(锚点) 会改变元素的定位轴点(每个轴取值0-1):
lua
frame.AnchorPoint = Vector2.new(0.5, 0.5) -- pivot at center
frame.Position = UDim2.fromScale(0.5, 0.5) -- truly centered on screenResponsive Design
响应式设计
Prefer scale over offset so UI adapts to all screen sizes.
lua
button.Size = UDim2.fromScale(0.2, 0.07)
button.Position = UDim2.new(0.4, 0, 0.85, 0)
-- Prevent distortion with UIAspectRatioConstraint
local arc = Instance.new("UIAspectRatioConstraint")
arc.AspectRatio = 4 -- width:height = 4:1
arc.Parent = button优先使用scale而非offset,这样UI可以适配所有屏幕尺寸。
lua
button.Size = UDim2.fromScale(0.2, 0.07)
button.Position = UDim2.new(0.4, 0, 0.85, 0)
-- Prevent distortion with UIAspectRatioConstraint
local arc = Instance.new("UIAspectRatioConstraint")
arc.AspectRatio = 4 -- width:height = 4:1
arc.Parent = buttonTweenService Animations
TweenService动画
lua
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local menuFrame = script.Parent
local function openMenu()
TweenService:Create(menuFrame, tweenInfo, {
Position = UDim2.new(0.05, 0, 0.1, 0)
}):Play()
end
local function closeMenu()
TweenService:Create(menuFrame, tweenInfo, {
Position = UDim2.new(-0.5, 0, 0.1, 0)
}):Play()
end
-- Animated progress bar
local function setProgress(bar, pct)
TweenService:Create(bar, TweenInfo.new(0.2), {
Size = UDim2.new(pct, 0, 1, 0)
}):Play()
endlua
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local menuFrame = script.Parent
local function openMenu()
TweenService:Create(menuFrame, tweenInfo, {
Position = UDim2.new(0.05, 0, 0.1, 0)
}):Play()
end
local function closeMenu()
TweenService:Create(menuFrame, tweenInfo, {
Position = UDim2.new(-0.5, 0, 0.1, 0)
}):Play()
end
-- Animated progress bar
local function setProgress(bar, pct)
TweenService:Create(bar, TweenInfo.new(0.2), {
Size = UDim2.new(pct, 0, 1, 0)
}):Play()
endLocalScript Placement
LocalScript放置位置
| Location | Notes |
|---|---|
| Cloned into |
| Runs once, not reset on respawn; good for persistent managers |
| Re-runs each spawn; suited for character-dependent UI |
lua
-- Safe pattern: wait for character
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid.HealthChanged:Connect(function(health)
-- update health bar
end)| 位置 | 说明 |
|---|---|
| 玩家加入时克隆到 |
| 仅运行一次,重生时不会重置;适合常驻管理器逻辑 |
| 每次玩家重生时重新运行;适合依赖角色的UI逻辑 |
lua
-- Safe pattern: wait for character
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid.HealthChanged:Connect(function(health)
-- update health bar
end)ResetOnSpawn
ResetOnSpawn属性
lua
screenGui.ResetOnSpawn = false -- persist across respawns (inventory, settings)
screenGui.ResetOnSpawn = true -- re-create on respawn (respawn timer) — defaultlua
screenGui.ResetOnSpawn = false -- 重生时保留GUI(适合背包、设置界面)
screenGui.ResetOnSpawn = true -- 重生时重新创建GUI(适合重生计时器)—— 默认值Common Patterns Quick Reference
常见模式速查
| Pattern | Key Setup |
|---|---|
| Full-screen overlay | |
| Bottom-center HUD bar | |
| Padded list | |
| Scrollable list | |
| Rounded corners | |
| Scaled text | |
| Dynamic frame height | |
| Health bar | Nested frames: outer = background, inner tweened by |
| Name tag | |
| 模式 | 核心配置 |
|---|---|
| 全屏覆盖层 | |
| 底部居中HUD栏 | |
| 带内边距的列表 | Frame中嵌套 |
| 可滚动列表 | |
| 圆角效果 | |
| 自适应缩放文本 | TextLabel/TextButton开启 |
| 动态高度框架 | 设置 |
| 血量条 | 嵌套框架:外层为背景,内层通过 |
| 名称标签 | 头部挂载 |
Common Mistakes
常见错误
| Mistake | Fix |
|---|---|
| GUI disappears on respawn | Set |
| UI looks wrong on mobile | Use |
Script can't find | Use |
| Tween doesn't run | Ensure the property is tweenable; |
| BillboardGui visible through walls | Verify |
| Read it inside |
| Clicks pass through overlapping frames | Add a transparent input-blocking Frame or set |
| SurfaceGui flickers | Set |
| Text tiny on mobile | Set |
| UI hard to test on mobile | Use Studio's Device Emulator (Test tab → Device) to preview layouts |
| 错误 | 修复方案 |
|---|---|
| GUI在玩家重生时消失 | 设置 |
| UI在移动端显示异常 | 使用 |
脚本找不到 | 使用 |
| 补间动画不运行 | 确认属性支持补间; |
| BillboardGui可穿墙显示 | 检查 |
首帧 | 在 |
| 点击事件穿透重叠框架 | 添加透明的输入拦截Frame,或设置 |
| SurfaceGui闪烁 | 设置 |
| 移动端文本过小 | 开启 |
| UI难以在移动端测试 | 使用Studio的设备模拟器(测试选项卡→设备)预览布局 |