Attempt to index nil with 'TeamColor'

I’m trying to make a main menu GUI where if you press play without picking a team the JobFrames GUI pops up, and if you select the Jobs menu in the main menu, and press play it just removes the gui


local canSpawn = Instance.new("BoolValue")

canSpawn.Name = "canSpawn"

local Players = game:GetService("Players")

local Teams = game:GetService("Teams")

local items = script.Parent:GetChildren()

local plr = script.Parent.Parent.Parent.Parent

local spawns = game.Workspace.Folder:GetChildren()

for i,v in pairs(items) do

if v:IsA("TextButton") then

table.insert(newTable,v)

end

end

for i,v in pairs(newTable) do

v.MouseButton1Click:Connect(function()

local t = Teams:FindFirstChild(v.Text)

plr.Team = t

script.Parent.Parent.Frame.Visible = true

if spawns[i].TeamColor == t.TeamColor then

plr.Character:MoveTo(spawns[i].Position + Vector3.new(0,10,0))

end

end)

end

Error is on this line: if spawns[i].TeamColor == t.TeamColor then

The error means that spawns[i] doesn’t exist. Double-check with what the variable i is and if spawns have the said children that i is referring to.

Maybe try if spawns[i].BrickColor = t.TeamColor then that might work.

Tried this and it worked for the one team.
Now when I try it for the other team it doesn’t change the team or tp the player to the spawner.
Both the Spawner and Team’s Team color is the same. So is the name of the button and the team name.


Code:

local newTable = {}
local canSpawn = Instance.new("BoolValue")
canSpawn.Name = "canSpawn"
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local items = script.Parent:GetChildren()
local plr = script.Parent.Parent.Parent.Parent
local spawns = game.Workspace.Folder:GetChildren()
for i,v in pairs(items) do
	if v:IsA("TextButton") then
		table.insert(newTable,v)
	end
end
for i,v in pairs(newTable) do
	v.MouseButton1Click:Connect(function()
		local t = Teams:FindFirstChild(v.Text)
		plr.Team = t
		for i=1, #spawns do
			if spawns[i].TeamColor == t.TeamColor then
				plr.Character:MoveTo(spawns[i].Position + Vector3.new(0,10,0))
			end
			script.Parent.Parent:Destroy()
		end
	end)
end```

== is to check if the values are equal

= makes the values equal

== is the right operator for this situation.

ok further more so ONLY that team breaks, I added more and I’ve checked the team color and the naming again and again.

more weirdness, it worked when I put it out of my testing environment and into the game

Yep, I know. I just couldn’t think straight at the time of me posting that.

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local Plr = script:FindFirstAncestorWhichIsA("Player")
local Char = Plr.Character or Plr.CharacterAdded:Wait()
local Folder = script.Parent
local Items = Folder:GetChildren()
local SpawnFolder = workspace:WaitForChild("Folder")
local Spawns = SpawnFolder:GetChildren()

for i, v in pairs(Items) do
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			local Team = Teams:FindFirstChild(v.Text)
			if Team then
				Plr.Team = Team
				for i, v in pairs(Spawns) do
					if v.BrickColor == Team.TeamColor then
						Char:MoveTo(v.Position + Vector3.new(0, 10, 0))
					end
				end
			end
		end)
	end
end

If you need any assistance regarding understanding any of the changes I’ve implemented don’t hesitate to ask.

1 Like