So here’s my username code:
local RunService = game:GetService("RunService")
local baseName = "Draw Name "
local registered = {}
local function unregister(name)
if typeof(name) == "Instance" then name = name.Name end
RunService:UnbindFromRenderStep(baseName..name)
if registered[name] then registered[name]:Destroy() end
end
function register(player:Player)
if player == game.Players.LocalPlayer then return end
local label = script.Username:Clone()
label.Text.Text = player.Name
label.Name = player.Name
label.Text.TextColor = player.TeamColor
repeat wait() until player.Character
local char = player.Character
local name = player.Name
if registered[name] then unregister(name) end
label.Parent = game.Players.LocalPlayer.PlayerGui
label.Adornee = char:WaitForChild("Head")
local debounce = false
registered[name] = label
RunService:BindToRenderStep(baseName..name, Enum.RenderPriority.Camera.Value - 1, function()
local camera = workspace.CurrentCamera
if not char or not char:FindFirstChild("Head") or not label or not label:FindFirstChild("Text") then
warn("Sanity check failed", char, char:FindFirstChild("Head"), label, label:FindFirstChild("Text"))
unregister(name)
return
end
local distance = (camera.CFrame.Position-char.Head.Position).Magnitude
local size = 23-(5*math.clamp(math.floor(distance/25), 0, 3))
if distance > 90 then
local transparency = math.clamp((distance-90)/10, 0, 1)
label.Text.TextTransparency = transparency
end
label.Size = UDim2.new(label.Size.X.Scale, label.Size.X.Offset, 0, size)
end)
end
function playerAdded(player)
if player == game.Players.LocalPlayer then return end
if player.Character then register(player) end
player.CharacterAdded:Connect(function()
register(player)
end)
player.CharacterRemoving:Connect(function()
unregister(player)
end)
player:GetPropertyChangedSignal("TeamColor"):Connect(function()
local label = registered[player.Name]
if label and label:FindFirstChild("Text") then
label.Text.TextColor = player.TeamColor
end
end)
end
repeat task.wait() until game:IsLoaded()
for i,player in pairs(game.Players:GetPlayers()) do
playerAdded(player)
end
game.Players.LocalPlayer.CharacterAdded:Connect(function() -- Fixes some weirdness
for i,player in pairs(game.Players:GetPlayers()) do
unregister(player)
task.wait()
register(player)
end
end)
game.Players.PlayerAdded:Connect(playerAdded)
And a model with a premade billboard: https://create.roblox.com/marketplace/asset/15274947357/Custom-username-with-scaling
The goal of the script is to replicate how default usernames work where they shrink in a step-wise pattern the further you get + the fading
I can’t help but feel that this code is probably a bit flawed. Please give me ideas for improvements.