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
game.Players.PlayerAdded:Connect(function(plr)
	wait()
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
	Team:GetPropertyChangedSignal('Value'):Connect(function()
		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)
		else
			Team.Value = 'None'
	end
end)
	
end
wait()
plr.CharacterAdded:Connect(attach)
wait()

end)
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
Team:GetPropertyChangedSignal:('Value'):Connect(function()

end)
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.

2 Likes