Local 'Team' a nil Value

Ok, so im creating a team tag when a player connects, and its a StringValue.
I have a script that adds a Billboard gui over a players head. But im getting the error:

Line 7: attempt to index field ‘Team’ a nil Value.


Tag Adder Code
local function attach(char) --takes char as the argument
	local Team = plr.Team
		local cTag = game:GetService('ServerStorage'):WaitForChild('TeamTag'):Clone()
		cTag.Parent = char.Head
		if Team.Value == 'Red' then
		char.Head.TeamTag.TeamName.Text = Team.Value
		char.Head.TeamTag.TeamName.TextColor3 = Color3.fromRGB(170,0,0)
		elseif Team.Value == 'Blue' then
		char.Head.Te.ValueamTag.TeamName.Text = Team.Value
		char.Head.TeamTag.TeamName.TextColor3 = Color3.fromRGB(0,76,255)
			Team.Value = 'None'

Adding the StringValue into the Player
local Team = Instance.new('StringValue')
	Team.Name = 'Team'
	Team.Value = 'None'
	Team.Parent = player
1 Like

When is the StringValue being added to the player? It seems to me as if the script looking for the value runs before the script that adds the value is ran.

Also I would strongly advise against having a value the same name as a property of player - you’d have to use FindFirstChild if you wanted to find the value then.

If the player’s not on a team, plr.Team will be nil. Then, Team:GetPropertyChangedSignal() would be the same thing as nil:GetPropertyChangedSignal(), which isn’t a property of nil. You can fix this by checking if Team’s not nil before your connection.

ok so would it be like:

if Team ~= nil then

1 Like

You’d need another end afterwards, but you’ve got the idea right, yeah.

I believe that your script errors since while trying to access the StringValue “Team” while doing plr.Team you end up using the value of the property (which I believe is nil for you) and not the StringValue, you can fix this by doing plr:FindFirstChild("Team") instead.

On another note,

  1. Make sure that the StringValue is created before the player spawns
  2. Why are you using StringValues for teams? Why not use the Team object provided by Roblox?
1 Like

Because i thought it would be the easy way to do it instead of creating teams and other stuff.

Using Team objects might actually be the easier way since it requires little to no scripting for initial setup. It also allows you to incorporate things like team chat and team displaying on leaderboards, decided spawns etc. without any major scripting.

I’d say try out using Roblox’s team once, I’m sure you’ll find it much easier to work with.