Anti Team-Kill not working

so basically I have a gun and I want the shots to not deal damage if the two players are on the same team. I have tried using equal to and not equal to but they both don’t work. I’ve tried using both Team and TeamColor and they both don’t work. Whenever I open a Local Server people who are on the same team still take damage and the “deal no damage” doesn’t print.

Code I'm using, there is obviously more code around it (the reason I'm using Player1 and Player2 is just to make testing easier at the moment)
local humanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid") 
if humanoid then
	if game.Players.Player1.TeamColor == game.Players.Player2.TeamColor --this is probably the broken part
		then print ("deal no damage")
	        else humanoid:TakeDamage(10) -- Damage.

without the rest of the code, this is too vague, please give us some more transparency

1 Like

main issue with that is that the rest of the code is 172 lines of code (I hijacked the FastCast free model gun to help me make a gun)
the gun does work under normal circumstances though

Simple! Just find out who the player is from the hitPart;

local Players = game:GetService("Players")
local Player = nil --how are you defining the player who shot the projectile?

	if hit.Parent:FindFirstChild("Humanoid") then
		local PlayerFromHit = Players:GetPlayerFromCharacter(hit.Parent)
		if PlayerFromHit then
			if PlayerFromHit.Team ~= Player.Team then
				--damage because they're not on the same team
				--no damage because they're on the same team

I am assuming you’re checking if the part is hit, which is why I didn’t provide anything else. If you know how you’re defining the player who shot the weapon, then just put the Player variable to that, and use the same checks I have implemented for you, then do the damage related stuff.

Hope this helped!

how exactly might I go about defining the person who shot the projectile? could I use something like:

local Player = Tool.Parent.Parent -- the tool then the backpack of the player then the player

or would that not work?

This might be able to work, but the tool gets parented to the Player’s character. So you’d have to get player from character on tool equipped.

oh yeah… I forgot that whenever you equip a tool it’s removed from your backpack, Is there a way to get player from character? or are there other ways to define player?
this is NOT a local script by the way

1 Like

Yes there is,

You could simply just do this I guess

local Player= Players:GetPlayerFromCharacter(script.Parent.Parent) --not sure if it will work

How do you have the client detecting the mouse events, if you’re firing the server to display the bullets, just get the player from the event instead of the parent.

1 Like