How do you create anti-team kill?

I’m making a sword, it works but I’m trying to make it so you can’t kill players on the same team as you, I’ve searched up YT tutorials but got nothing.
This is what i’ve done

debounce = false

function onTouch(part) 
	local humanoid = part.Parent:FindFirstChild("Humanoid") 
	if debounce == false then
		if (humanoid ~= nil) then
			humanoid.Health = humanoid.Health - 35
			debounce = true
		end 
		if debounce == true then
			wait(2)
			debounce = false
		end
	end
end

script.Parent.Touched:connect(onTouch)
3 Likes

use this as your script:

function onTouch(part) 
	local hitplayer = game.Players:GetPlayerFromCharacter(part.Parent)
	local hitter = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)--change this to get the player it is being used by.
	if hitplayer.TeamColor ~= hitter.TeamColor then
		local humanoid = part.Parent:FindFirstChild("Humanoid") 
		if debounce == false then
			if (humanoid ~= nil) then
				humanoid.Health = humanoid.Health - 35
				debounce = true
			end 
			if debounce == true then
				wait(2)
				debounce = false
			end
		end
	end
end

script.Parent.Touched:connect(onTouch)
4 Likes

This.

if LocalPlayer.Team == TargetPlayer.Team then --team is a singular object in memory, so this works
  --handle team hit
  return --end the callback
end
1 Like

it should be

if LocalPlayer.Team ~= TargetPlayer.Team then
-- all of your script goes here
end

What if I want to add animation. I keep try and fix the sword still not working for animation