Having trouble finding a player's team

I am trying to check if a player is on a certain team, but I’m getting an error. Here’s the script:

local part = script.Parent

local replicatedStorage = game:GetService("ReplicatedStorage")
local increase = replicatedStorage.IncreaseVisibility
local decrease = replicatedStorage.DecreaseVisibility

local Teams = game:GetService("Teams")
local gravel = Teams.Gravel

local Players = game:GetService("Players")

part.Touched:Connect(function(otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid then
		local plr = character.Parent
		if plr then
			if plr.TeamColor == BrickColor.new("Bright red") then
				decrease:FireClient(plr)
			end
		end
	end
end)

It’s saying that TeamColor is not a valid member of workspace. How do I fix this?

I think I know what is going on here. Normally by intuition when we do “Player.Character” we believe that the character is a child of the player. However, this is not the case. The Character property on player is a link that directs to the real player’s character. If you wish to get the player replace:

local plr = character.Parent

--with

local plr = game:GetService("Players"):GetPlayerFromCharacter(character);
1 Like

character.Parent is not player

local part = script.Parent

local replicatedStorage = game:GetService("ReplicatedStorage")
local increase = replicatedStorage.IncreaseVisibility
local decrease = replicatedStorage.DecreaseVisibility

local Teams = game:GetService("Teams")
local gravel = Teams.Gravel

local Players = game:GetService("Players")

part.Touched:Connect(function(otherPart)
	local character = otherPart.Parent
	
	if character:FindFirstChild("Humanoid") then
        local humanoid = character:FindFirstChild("Humanoid")
		local plr = game.Players:GetPlayerFromCharacter(character)
		if plr then
			if plr.TeamColor == BrickColor.new("Bright red") then
				decrease:FireClient(plr)
			end
		end
	end
end)
1 Like

local plr = game:GetService('Players'):FindFirstChild(character.Name, false)

1 Like

Player.Character is a property not a model.



local part = script.Parent

local replicatedStorage = game:GetService("ReplicatedStorage")
local increase = replicatedStorage.IncreaseVisibility
local decrease = replicatedStorage.DecreaseVisibility

local Teams = game:GetService("Teams")
local gravel = Teams.Gravel

local Players = game:GetService("Players")

part.Touched:Connect(function(otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid then
		local plr = Players:GetPlayerFromCharacter(character)
		if plr then
			if plr.TeamColor == BrickColor.new("Bright red") then
				decrease:FireClient(plr)
			end
		end
	end
end)
1 Like

Better to use GetPlayerFromCharacter instead.

A player’s name may be used by some other instance, i.e; ‘Model’.

4 Likes

Touched event gets the BasePart who touches it so u trying to get the player from character it’s like :

local part = script.Parent

part.Touched:Connect(function(otherPart) --otherPart returns the otherparts who touches this part 

local plr = game.Players:GetPlayerFromCharacter(otherPart.Parent) 
if plr then -- to make sure if real player and found him 

-- here u got the player to get the player character use plr.Character and do your stuff
-- plr.TeamColor ~~ plr.Team ...
end
end)

otherPart.Parent returns the parent of part that touches this part.
so the character have parts in it to get the character using otherPart.Parent

Hope it helps.

1 Like