How can I assign a team to a humanoid?

Hey guys,

Just wondering if it is possible to assign a team color to a humanoid so that only the opposite team of that humanoid can damage it.

Is it? If so, how can I do that?

Thanks in advance.

1 Like

I believe you could just name each humanoid the team name, and check if humanoids on the damage receiving end don’t have the same team name as you do.

3 Likes

A team is always assigned to the player, not the humanoid.

2 Likes

@KeysOfFate but I have a humanoid in a part, and i want to give that humanoid a team so if its on the blue team blue players can’t damage the humanoids health, and vice versa for the other team

1 Like

Same idea applies. Just name the humanoid whatever team it is associated with, and check if the team name is different before damaging it.

I believe this can be achieved by getting the person who shoots/uses a tool via an event with a parameter, like how Players.PlayerAdded:Connect(function(Player)) grabs the player, except you need to detect when they click or use the item. After getting the player, just check if the person who gets hit is not on the same team.

MyTeam ~= PlayerTeam

And if this is true, inflict damage.

You never needed to change a humanoid’s team somehow. Just get normal teams between players.

is there a simpler way? seems like i would have to make every weapon adapt to this.

should i use something other than a humanoid?

Unfortunately, this is the only way I can think of.

If possible, there might be a way to check player’s teams at the start of the game using a table, and somehow make the people on that same team take no damage from others of the same team.

You can always try making a module script and creating a function that you can simply put in every one of your weapons, at least that’s how I think module scripts work. I barely ever hear about them and I have no knowledge on module scripts whatsoever.

You cannot assign a team to a humanoid, what you need to do is when the player joins add them to a team, then to get the team from a humanoid you would do:

local Character = Humanoid.Parent
local Player = game:GetService("Players"):GetPlayerFromCharacter(Character)

if Player then
   --Player.Team to check the team
end

so this is a script in the humanoid, giving the humanoid a team, and that can prevent team damage?

Heres how I would do it
I would name the Humanoid , Blue or Red for example.

Then inside the weapon script. if plr.Team == game.Teams.Blue then
if Humanoid.Name == “Blue” then
return
end
end

Honestly I like inserting StringValues into players, when they join the game like this

game.Players.PlayerAdded:Connect(function(plr)
local hay=game.ReplicatedStorage.Var:Clone
hay.Parent=plr
end)

Then I can find it by calling the player then the var then the value
For example
plr.var.Value

@EletricalSpy this where its at bois

U said u don’t want NPC to be attacked so I was saying
if Npc:findFirstChild(localplr.Team.Name) then
return
end
– this return makes it so it wont dmg.

no i want npc to have a team and people on the team cant damage the npc, but people on the other team with sword can damage npc

You can’t make a NPC have a Team.

assign a humanoid and give it a brick value that matches one of team colour brick values in the game. I’ve already done it. The problem is filtering damage from teammates.

edit: i just got it working, i can now damage npc that is not on my team without hurting ones on my own

A player’s sword and a player’s NPC change with the player’s team

The file contains all the details in the video
Team change sword and NPC.rbxl (40.4 KB)

Do not hesitate to ask any questions. I love to learn and help others
If there are errors, I am glad you mentioned them in the comments

2 Likes

I am very happy that I am waiting for your comments :grinning:

1 Like