Player.TeamColor returning nil

I am trying to make a certain nametag appear above a player’s head based on group rank and team. this is part of my script:

Script excerpt
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		while true do
			local groups = groupService:GetGroupsAsync(plr.UserId)
			local inGroup = false
			local groupRole
			local player = game.Players.LocalPlayer

			if player.TeamColor == BrickColor.new("Pastel light blue") then
				for key, group in pairs(groups) do
					if group.Id == 3059674 then
						inGroup = true
						groupRole = group.Role
						break
					end
				end

			
			elseif player.TeamColor == BrickColor.new("Really red") then
				for key, group in pairs(groups) do
					if group.Id == 6164618 then
						inGroup = true
						groupRole = group.Role
						break
					end
				end
			end
			
			updateTag(char, inGroup, groupRole)
			wait(5)
		end
	end)
end)

And this is the error im getting:

Why is this error coming out and the script not working?

Thanks in advance!

Greetings, from what I see you are trying to call the following line in a Server Script

local player = game.Players.LocalPlayer

The problem is that you can get the “LocalPlayer” only in LocalScripts, in this case you should replace all the “player” with “plr” and remove that line

2 Likes

You’re using a combination of game.Players.LocalPlayer, which is the proper way of identifying a player on the client, and the plr variable from your playeradded event.

You already have the player defined with plr, you don’t need to do the player defining:
“local player = game.Players.LocalPlayer”

try this:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		while true do
			local groups = groupService:GetGroupsAsync(plr.UserId)
			local inGroup = false
			local groupRole

			if plr.TeamColor == BrickColor.new("Pastel light blue") then
				for key, group in pairs(groups) do
					if group.Id == 3059674 then
						inGroup = true
						groupRole = group.Role
						break
					end
				end

			
			elseif plr.TeamColor == BrickColor.new("Really red") then
				for key, group in pairs(groups) do
					if group.Id == 6164618 then
						inGroup = true
						groupRole = group.Role
						break
					end
				end
			end
			
			updateTag(char, inGroup, groupRole)
			wait(5)
		end
	end)
end)