Name Tag Lagging Server

This is the script to my name tag inserter.

The issue starts to happen once the server has been up for a while.

local Device = {
	["Computer"] = "💻",
	["Phone"] = "📱",
	["Console"] = "🎮"
}

local Players = game:GetService("Players")
local Run = game:GetService("RunService")
local Admin = require(game.ReplicatedStorage.AdminWhitelist)
local function GetStatusText(status,badges)
	if status.Value == true then
		return {"",Color3.fromRGB(255, 255, 255)}
	elseif status.Value == false and badges.Value ~= 0 then
		return {"(counting)",Color3.fromRGB(255, 255, 255)}
	else
		return {"(starting to count...)",Color3.fromRGB(255, 238, 0)}
	end
end

local function getDevice(player)
	if player:GetAttribute("Device") ~= nil then
		return player:GetAttribute("Device")
	end
	local device = game.ReplicatedStorage.Events.GetDevice:InvokeClient(player)
	player:SetAttribute("Device",device)
	return device
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(model)
		local head = model:WaitForChild("Head")
		local humanoid = model:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.NameDisplayDistance = 0
		end
		if head then
			local title = script:FindFirstChild("Title"):Clone()
			title.Parent = head
			title.Username.Text = player.DisplayName
			local tabbedout
			local tabbed
			tabbed = game.ReplicatedStorage.Events.Tabbed.OnServerEvent:Connect(function(Player)
				if Player == player then
					title.Username.Text = Device[getDevice(player)]..player.DisplayName
					title.Username.TextColor3 = Color3.fromRGB(255, 255, 255)
					if table.find(Admin,player.UserId) then
						title.Username.Text = "[DEVELOPER] "..Device[getDevice(player)]..player.DisplayName
						title.Username.TextColor3 = Color3.fromRGB(0, 221, 255)
					end
				end
				if not player and tabbed then
					tabbed:Disconnect()
				end
			end)
			
			tabbedout = game.ReplicatedStorage.Events.TabbedOut.OnServerEvent:Connect(function(Player)
				if Player == player then
					title.Username.Text = Device[getDevice(player)]..player.DisplayName.. " [AFK]"
					title.Username.TextColor3 = Color3.fromRGB(120, 120, 120)
					if table.find(Admin,player.UserId) then
						title.Username.Text = "[DEVELOPER] "..Device[getDevice(player)]..player.DisplayName.. " [AFK]"
						title.Username.TextColor3 = Color3.fromRGB(0, 117, 130)
					end
				end
				if not player and tabbedout then
					tabbedout:Disconnect()
				end
			end)
			
			local badges = player:WaitForChild("leaderstats"):WaitForChild("badges")
			if badges then
				title.Badges.Text = badges.Value.. " badges"
				badges:GetPropertyChangedSignal("Value"):Connect(function()
					title.Badges.Text = badges.Value.. " badges"
				end)
			end
			local status = player:WaitForChild("Statistics"):WaitForChild("FinishedCounting") 
			if status then
					 badges:GetPropertyChangedSignal("Value"):Connect(function()
					local textProperties = GetStatusText(status,badges)
					title.Status.Text = textProperties[1]
					title.Status.TextColor3 = textProperties[2]
					 end)
				local textProperties = GetStatusText(status,badges)
				title.Status.Text = textProperties[1]
				title.Status.TextColor3 = textProperties[2]
			end
		end
	end)
	
end)

Activity level :


Here are some of the Name Tag activity levels that have been going on. If anyone can find a solution that would help a lot.

I see quite a couple of things that cause the server to lag

  1. You are creating connections to tabbed out and tabbed everytime the character is added. Also if the player exists then the event never gets disconnected (solve it by disconnecting regardless of player existence)
  2. You are registering a “propertychanged” connection every time the character is added
  3. Just remove the Waitforchild’s, adding unnecessary waiting time
  4. Constantly updating the gui things for every firing of the remove event

You want to run some code on different cores. Use spawn or coroutines to divide work among cores on the device.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.