Username script

A lot of things to improve:

1. Reduce Loops

Loops tank performances as it uses a lot of CPU power. It stays on the tasks/frame until it is completed. It is actively using the CPU and memory.

Read more:
Events | Roblox Lua Programming Formalities Guide - Resources / Community Resources - Developer Forum | Roblox

Loop 1

--try
if not game:IsLoaded() then
     game.Loaded:wait()
end
--instead of
repeat task.wait() until game:IsLoaded()

Read more:
DataModel | Documentation - Roblox Creator Hub
Events | Roblox Lua Programming Formalities Guide - Resources / Community Resources - Developer Forum | Roblox

Loop 2

Why are looping until something is true? When you can use :Wait() function.

-- Use
if not player.Character then
    player.CharacterAdded:Wait()
end

-- Instead of
repeat wait() until player.Character

Read more:
Player | Documentation - Roblox Creator Hub
Events - Roblox Lua Programming Formalities Guide - Resources / Community Resources - Developer Forum | Roblox

2. Define all services at the top

local Players = game:GetService("Players")
local RunService = Game:GetService("RunService")
-- etc

Once you define it, use that assignment.
Read more:
Services | Documentation - Roblox Creator Hub
Services | Roblox Lua Programming Formalities Guide - Resources / Community Resources - Developer Forum | Roblox

3. Don’t use wait()

It is not efficient, and can cause performance issues. Use task.wait()

-- Use
task.wait(1)

-- Instead of
wait(1)

Read more: task | Documentation - Roblox Creator Hub

4. Is your script overcomplexed?

Why are you using? RunService:BindToRenderStep() Why is the local client worrying about another client (or other clients)? It looks like you are trying to create a nametag program, that is doing a math calculation, this eating up the performance of the client.

Here is what I would do:
This was programmed for the client! :smiley:

--!strict
-- Global Assignments / Constants (PascelCase) # Assignments that doesn't change
local Players = game:GetService("Players")

-- Local Assignments (camalCase) # Assignments that will (or going to change)
local labelTemplate: BillboardGui | TextLabel | {["Text"]: TextLabel, ["Clone"]: any, ["MaxDistance"]: number} = script:WaitForChild("Username")-- :: BillboardGui

local connections = {}
function createNameTag(player: Player) : boolean
	connections[player.UserId] = {}
	local character: Model = player.Character or player.CharacterAdded:Wait()
	local label = labelTemplate:Clone()
	label.Text.Text = player.Name
	label.Parent = character
	label.Adornee = character:WaitForChild("Head")
	label.Name = player.Name -- IDK why you want that, but cool
	label.Text.TextColor = player.TeamColor
	label.Text.TextScalable = true
	table.insert(connections[player.UserId], player:GetPropertyChangedSignal("TeamColor"):Connect(function()
		label.Text.TextColor = player.TeamColor
	end))
	
	table.insert(connections[player.UserId], player.CharacterAdded:Connect(function()
		label.Parent = character
		label.Adornee = character:WaitForChild("Head")
	end))
	
	return true
end

labelTemplate.MaxDistance = 90 -- Type error, cause Roblox said so... IDK why it mad

Players.PlayerRemoving:Connect(function(player: Player)
	for _,connection in connections[player.UserId] do
		if connection.Connected then
			connection:Disconnect()
		end
	end
	connections[player.UserId] = nil
end)

Players.PlayerAdded:Connect(createNameTag)

for _,player: Player in  Players:GetPlayers() do
	createNameTag(player)
end

Check out these posts to improve your programming skills

Roblox Lua Programming Formalities Guide - Resources / Community Resources - Developer Forum | Roblox
All about optimization - Resources / Community Tutorials - Developer Forum | Roblox
Type checking for beginners! - Resources / Community Tutorials - Developer Forum | Roblox

Sources

DataModel | Documentation - Roblox Creator Hub
Player | Documentation - Roblox Creator Hub
Events - Roblox Lua Programming Formalities Guide - Resources / Community Resources - Developer Forum | Roblox
Servuces - Roblox Lua Programming Formalities Guide - Resources / Community Resources - Developer Forum | Roblox
Services | Documentation - Roblox Creator Hub
task | Documentation - Roblox Creator Hub

1 Like