Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I find a player's team, can you help me?

Hello, i'm making a capture point script and I wan't to change the brickcolor to red if Red team captures the point, but it's not working.. can you help me?

local CapturePointA = game.Workspace.CapturePointA
function Touched(player)
	debounce = false
		if player.Team == "Red" then
			print 'Red person gross'
	end	
	end
CapturePointA.Touched:connect(Touched)
debounce = false


2 comments

0

if player.TeamColor==BrickColor.new('Bright red') then

0

Please wait while I explain the answer in full detail.

2 answers

✓ Accepted answer

[^1]: Players service, not the player object [^2]: The chart for converting brickcolor values is here

#What's the problem?

  • The debounce is unnecessary if you're using it this way
  • Touched returns the part, not the player
  • The team property is not called Team, it's called TeamColor
  • TeamColor isn't a string value, it's a BrickColor value
  • "Red" isn't a color. Use BrickColor.Red() or "Bright red" instead

#Getting the player from a part You can use a function of Players[^1]. You have to have the character as one of the parameters and it'll find the player that way.

--Wiki example
local character = game.Workspace.Player
local player = game.Players:GetPlayerFromCharacter(character)
 
if player then
    print("Player is " .. player.Name)
else
    print("Player doesn't exist!")
end

#TeamColor The property name is TeamColor. Not Team, and roblox is very sensitive to caps and small typos. Make sure you read through and fix all of them.


#Comparing BrickColors BrickColors are they're own datatype and is a type of color value. They must either be compared to another BrickColor value or they can be turned into other values[^2].

print(BrickColor.new() == 3)
print(BrickColor.new() == BrickColor.new())
print(BrickColor.White() == 0)

false

true

false

Use words such as "Name" or "Number" or "Color" to convert BrickColor to other values.


#Final Product

local CapturePointA = game.Workspace.CapturePointA
function Touched(part)
	local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
	if player and player.TeamColor.Name == "Bright red" then --Checked to make sure the player existed; converted the BrickColor to a string using "Name" so we can compare the values
		print 'Red person gross'
	end 
end
CapturePointA.Touched:connect(Touched)


Hope it helps!

1 comment

0

RIP you posted while I was typing the answer lol

Hello.

The reason why this script wasn't working was because 'Team' is not a valid member of the Player object. Also,you never added code that gets the player object from the character that touches the brick.

The fixed code is detailed below.

script.Parent.Touched:connect(function(h) --'h' is assigned to the object that touches the brick.
local Player=game.Players:GetPlayerFromCharacter(h.Parent) --We are getting the player from the character that hit the brick.We use 'h.Parent' because the Player's leg or arm will hit the brick.
if Player~=nil then --Checking to see if it was indeed a player that hit the brick.
if Player.TeamColor==BrickColor.new('Bright red') then --Checking to see if the player is in the red team.
print("EW A RED PERSON! GROSS!") --Yea,it is gross.
end
end
end)


If this is the answer you were looking for,upvote it and mark it as the answer!

2 comments

0

That code is disgusting. Please tab it.

0

Un-needed comment.

This discussion is preserved as it was. New answers, comments, and votes are closed.