Why do I get this error?

Hello! I have made a spawn point script that controls the player’s team. Whenever I play the game, I get this error, but everything works fine! How would I get rid of this error?

Error: 17:46:39.450 - ServerScriptService.SpawnPointControl:11: attempt to index nil with ‘TeamColor’

Script:

game.Players.PlayerAdded:Connect(function(player)
	
	player.Team = game.Teams.Obbyists --sets the player's team to the obbyists team when a player joins.
	
end)

local spawnpoints = workspace.Lobby.SpawnPoints:GetChildren() --variable for the spawn points

for _, spawnpoint in pairs(spawnpoints) do --loop going through the spawn points
	
	spawnpoint.TeamColor = game.Teams:FindFirstChild(spawnpoint.Name).TeamColor --finds the team with the same name as the spawn point and sets the spawn point's team color to the team's team color.
	
end
game.Players.PlayerAdded:Connect(function(player)
	player.Team = game.Teams.Obbyists --sets the player's team to the obbyists team when a player joins.
end)


local spawnpoints = workspace.Lobby.SpawnPoints:GetChildren() --variable for the spawn points
for _, spawnpoint in pairs(spawnpoints) do --loop going through the spawn points
	local Team = game.Teams:FindFirstChild(spawnpoint.Name)
	if Team then
		spawnpoint.TeamColor = Team.TeamColor --finds the team with the same name as the spawn point and sets the spawn point's team color to the team's team color.
	end
end
2 Likes