How would I be able to optimize this script?

This local script is for custom team-only nametags.

TeamNameLabel is a BillboardGui.
LOCAL_TeamTagsDisabled is a custom settings value that disables nametags.
LOCAL_InCutscene checks whether the client is viewing a cutscene so it disables nametags.

How can I optimize this script? I’m pretty sure this causes some performance issues on the client since my game runs with 70 players on a single server.

local plrs = game:GetService("Players");
local lclplr = plrs.LocalPlayer

local rs = game.ReplicatedStorage
local runS = game:GetService("RunService")

runS.Heartbeat:Connect(function()
    for i, plr in pairs(plrs:GetPlayers()) do
        if plr.Character and plr.Character:FindFirstChild("Head") and plr.Team == lclplr.Team and plr ~= lclplr then
            local check = plr.Character.Head:FindFirstChild("TeamNameLabel")
            if not check then
                local c = script.TeamNameLabel:Clone()
                c.Frame.name.TextColor3 = plr.TeamColor.Color
                c.Frame.name.Text = plr.Name
                c.Frame.Dot.BackgroundColor3 = plr.TeamColor.Color
                c.Parent = plr.Character.Head
            end
            if check then
                check.Frame.name.TextColor3 = plr.TeamColor.Color
                check.Frame.name.Text = plr.Name
                check.Frame.Dot.BackgroundColor3 = plr.TeamColor.Color
                if rs.LOCAL_TeamTagsDisabled.Value == true then
                    check.Enabled = false
                else
                    check.Enabled = true
                end
                if rs.LOCAL_InCutscene.Value == true then
                    check.Enabled = false
                end
            end
        end
        if plr.Character and plr.Character:FindFirstChild("Head") and plr.Team ~= lclplr.Team then
            local check = plr.Character.Head:FindFirstChild("TeamNameLabel")
            if check then
                check:Destroy()
            end
        end
    end
end)```

I don’t think that it has perfomance issues at all. Problem might be somewhere else. 70 players in a server causes lag if they are together.

If you want to optimize a script, ask yourself when is this code going to run and is there any way for me to run it less

In this situation, this code probably won’t affect performance that much (still not a very good solution in my eyes), it just changes the color of some UI every frame but if you want to improve it, there are much better ways of doing this

You only want to update the nametag in a few situations:
a. When a player’s character loads in
b. When TeamTagsDisabled is changed
c. When a player’s team changes
d. When the player is in a cutscene

For when a player’s character loads in you can connect to the Player.CharacterAdded event and then call some sort of function that updates the tag

For when the player joins a cutscene or has team tags disabled, you can use BoolValue’s .Changed event to call the update function when it changes

For when the player joins a different team you can use :GetPropertyChangedSignal("Team") to update the player’s tag when their team changes
.
Doing that will definitely improve things because you don’t have to update all the tags every frame, and now it’s probably only every few seconds or minutes.


Here’s a nitpick on how this script is formatted.

I don’t like how some developers use very short variable names that don’t give much information about what they represent.

The length of a variable really doesn’t matter. It doesn’t matter if you save 5 letters if your code becomes difficult to read. I’d rather be able to understand exactly which service you are referring to by rather than saving a few characters especially for these big variables that are used in all scopes.

Instead of rs or ts which could mean many different things, RunService, ReplicatedService, TestService, TextService, TweenService, I write out the entire name.

That is a smaller problem but I think you should look into fixing it.

A bigger problem is that you are repeating things a lot, remember your code should be DRY
In some cases you really need more variables for clarity for cleaner code.
Example: