名称标签
如何为角色添加名称标签。

Client/Index.lua
-- Function to add a Nametag to a Player
function AddNametag(player, character)
-- We don't need to see our own Nametag
if (player:IsLocalPlayer()) then return end
-- Try to get its character
if (character == nil) then
character = player:GetControlledCharacter()
if (character == nil) then return end
end
-- Spawns the Nametag (TextRender),
local nametag = TextRender(
Vector(), -- Any Location
Rotator(), -- Any angles
player:GetName(), -- Player Name
30, -- Text Size
Color.WHITE -- White Color
)
-- Attaches it to the character and saves it to the player's values
nametag:AttachTo(character)
nametag:SetRelativeLocation(Vector(0, 0, 250))
player:SetValue("Nametag", nametag)
end
-- Function to remove a Nametag from a Player
function RemoveNametag(player, character)
-- Try to get it's character
if (character == nil) then
character = player:GetControlledCharacter()
if (character == nil) then return end
end
-- Gets the Nametag from the player, if any, and destroys it
local text_render = player:GetValue("Nametag")
if (text_render and text_render:IsValid()) then
text_render:Destroy()
end
end
-- Adds a new Nametag to a character which was possessed
Character.Subscribe("Possess", function(character, player)
AddNametag(player, character)
end)
-- Removes the Nametag from a character which was unpossessed
Character.Subscribe("UnPossess", function(character, player)
RemoveNametag(player, character)
end)
-- When the Package loads, adds Nametags to the Players already in the server
-- (e.g. when you join a server with Players already playing, or when reloading the Package)
Package.Subscribe("Load", function()
for _, player in pairs(Player.GetPairs()) do
RemoveNametag(player)
AddNametag(player)
end
end)
提示
You can also use the Text3D class for 3D texts with depth, bevel and outline!