Why do I get the error Players.SadWowow21.PlayerGui.MenuGui.Frame.TeamSelectImage.Team1Button.LocalScript:5: attempt to index nil with 'TeamColor'?

I am making a gui that lets players select a team, when I test it I keep getting
07:33:34.225 - Players.SadWowow21.PlayerGui.MenuGui.Frame.TeamSelectImage.Team1Button.LocalScript:5: attempt to index nil with ‘TeamColor’

I am doing

local TeamImage = script.Parent.Parent
local Team1Button = script.Parent

function onClick(player)
	player.TeamColor = BrickColor.new("Parsley green") 
	TeamImage.Visible = false
end

Team1Button.MouseButton1Click:Connect(onClick)

and I have no clue why this is not working

1 Like

Because the .MouseButton1Click event does not provide the player who clicked as a parameter. Just define local player = game:GetService("Players").LocalPlayer at the top of your script.

2 Likes

it kind of works, it makes it not visible but doesn’t change the player’s team

1 Like

Did you check if script.Parent.Parent is not the GUI, but the image?

2 Likes

Is this a localscript? you need to be changing the team on the server by using a normal script.

Edit:

Here’s a video showing you how to do it with remote events: https://streamable.com/wzi5t4

Read Vong’s article / talk to me in DM if you have questions about it

And here’s how you’d do it with a server script inside the gui:

I put the script directly inside of the gui as you can see here:
image

The script is inside a gui inside the player so that’s how we get the player. It is a normal script so it’s changing the team on the server (for everyone) instead of locally (only changing it on your computer) in a local script.

Here’s the script:

local teams = game:GetService("Teams")

local gui = script.Parent
local player = gui.Parent.Parent

local TeamImage = gui.Frame.TeamSelectImage
local Team1Button = TeamImage.Team1Button

function onClick()
	player.Team = game.Teams["MyTeamName"]
	TeamImage.Visible = false
end

Team1Button.MouseButton1Click:Connect(function()
	onClick()
end)
1 Like

That is because changing the TeamColor on the client does not replicate to the server. You will need to use RemoteEvents.

1 Like

oh yeah, I just realized I am using a local script

1 Like