What am I doing wrong in this line of code

local chara = players:FindFirstChildOfClass("Player")
		for _, charaplayers in pairs (chara.Parent:GetChildren()) do
			print(charaplayers)
			local teams = charaplayers:FindFirstChildOfClass("Player")
			local team = teams
			if team.TeamColor == BrickColor.new("Bright red") then
				local chara1 = charaplayers.Character
				chara1:MoveTo(Vector3.new(52.759, 77.66, 639.622))
				print("SUCCESS!")
			end
		end

This is a large snippet of code. The reason why I excluded a lot is because this heavily shortened script has a error at if team.TeamColor == BrickColor.new("Bright red") then. It is telling me that im indexing null and I do not know what method solves this problem to get a player’s teamColor so I can find the player’s model through character to move their position. I do not know how to get that singular player with that teamColor properly and while I did manage to do it in some of my older posts (differently), this one is a different problem revolving around getting a singular player’s character (charaplayers.Character) model based on their TeamColor and has been a constant struggle for me to debug. If someone would like to help, please do so. I’d greatly appreciate it!

Well, I assume you’re looking for the player’s character in workspace and well the correct class is the Model class, not the Player class.

Anyways, the player’s character is technically not a child of the player so it wouldn’t work anyways.

Why do you need the “team” variable when it’s the same thing as the “teams” variable??

If I badly explained it I apologize. This script attempts to locate certain players with a certain TeamColor (that being Bright red) and after the conditions have passed, the player’s character in workspace will be teleported to another position.

local team = teams wasn’t finished as I was trying to get the player from teams but due to my thinking that I don’t know how to solve this problem, I went with posting it on DevForum anyways.

1 Like

Ah yeah I understood that but why don’t you try replacing this line:

charaplayers:FindFirstChildOfClass(“Player”)

With:

charaplayers.Character

Unless you’re trying to do something else other than getting the character

You get a player from the players and use that variable to pretty much go back to the players and go through all of them, so just shorten it to

for _, charaplayers in pairs(players:GetPlayers()) do --GetPlayers() only gets players and ignores other things in there

You already have the player so you don’t need to try and find it again, and setting team equal to the same thing for no purpose is redundant. (finding a player under a player is nil, so that’s probably the error)

		for _, charaplayers in pairs (players:GetPlayers()) do
			print(charaplayers)
			if charaplayers.TeamColor == BrickColor.new("Bright red") then
				local chara1 = charaplayers.Character
				chara1:MoveTo(Vector3.new(52.759, 77.66, 639.622))
				print("SUCCESS!")
			end
		end
1 Like

Well it worked. Thank you! The only thing I’m not happy about is the fact that I forgot that :GetPlayers() exists but other than that at least I know one other line of code I can use. I feel pretty dumbfounded that Roblox has some really useful code lines that could be used but aren’t easy to find. This is a valuable lesson though.

Yup, you gotta search for a bunch of them, but you’ll get the hang of it after a while! :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.