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);
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)
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)
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