Cannot figure out how to utilize Anti-Team Kill with a weapon

The title of the topic explains my issue really

TL;DR at the bottom

Currently I am having issues with a tool I was provided to host an event for a community, the ability to add and remove team-kill. This is due to the fact that this tool, much like other combat tools is more “advanced” with animations to go along with it, as well as its own specific damage values for each hit, which is a right, left, and over hit.

Looks like this:
https://i.gyazo.com/4fba8f97ce8f2ef4f857221d4d3671bc.mp4

I’ve looked at previous DevForum posts and YouTube videos to try and find a source to the problem, and it comes around the “blow” function that the SwordScript utilizes.

My script looks like this:

function blow(hit)
	if enabledToDamage == false then return end
	enabledToDamage = false
	if (hit.Parent == nil) then enabledToDamage = true return end -- happens when bullet hits sword
	local humanoid = hit.Parent:findFirstChild("Humanoid")
	local vCharacter = Tool.Parent
	local vPlayer = game.Players:playerFromCharacter(vCharacter)
	local hum = vCharacter:findFirstChild("Humanoid") -- non-nil if tool held by a character
	if humanoid~=nil and humanoid ~= hum and hum ~= nil then
		-- final check, make sure sword is in-hand
		local right_arm = vCharacter:FindFirstChild("Right Arm")
		if (right_arm ~= nil) then
			local joint = right_arm:FindFirstChild("RightGrip")
			if (joint ~= nil and (joint.Part0 == sword or joint.Part1 == sword)) then
				tagHumanoid(humanoid, vPlayer)
				humanoid:TakeDamage(damage)
				wait(1)
				untagHumanoid(humanoid)	
			else
				enabledToDamage = true
			end
		else
			enabledToDamage = true
		end
	else
		enabledToDamage = true
	end
end

I’ve looked at an example of an Anti-Team Kill sword on the Toolbox, and the output is reletively the same at the beginning, but then at the “if (joint ~= nil and (joint.Part0 == sword or joint.Part1 == sword)) then” line, it changes the values.

function blow(hit)
	if (hit.Parent == nil) then return end -- happens when bullet hits sword

	local humanoid = hit.Parent:findFirstChild("Humanoid")
	local vCharacter = Tool.Parent
	local vPlayer = game.Players:playerFromCharacter(vCharacter)
	local hum = vCharacter:findFirstChild("Humanoid") -- non-nil if tool held by a character
	if humanoid~=nil and humanoid ~= hum and hum ~= nil then
		-- final check, make sure sword is in-hand

		local right_arm = vCharacter:FindFirstChild("Right Arm")
		if (right_arm ~= nil) then
			local joint = right_arm:FindFirstChild("RightGrip")
			if (joint ~= nil and (joint.Part0 == sword or joint.Part1 == sword)) then
				if game.Players:GetPlayerFromCharacter(humanoid.Parent).TeamColor ~= Team or game.Players:GetPlayerFromCharacter(humanoid.Parent).Neutral == true then
					tagHumanoid(humanoid, vPlayer)
					humanoid:TakeDamage(damage)
					wait(1)
					untagHumanoid(humanoid)
				end
			end
		end


	end
end

If I attempt to change the code from after the “if (joint ~= nil and (joint.Part0 == sword or joint.Part1 == sword)) then” line, my entire tool breaks and it comes useless.

I’ve also tried adding in a section of code in the blow function which I found on a post:

if Player.Team ~= TargetPlayer.Team then

-- Do Damge to the TargetPlayer

end

But it simply made it so that the tool did function with the animations, BUT the damage rendered useless.

TL;DR
Don’t know how to script an anti-team kill weapon, I want to know how to fix it.

Questions
Would this have something to do with the way I set up my teams?
Would this script have to be re-done so that values of the teams are also included?

I am not a scripter at all, so bear with my lack of knowledge on the subject.

So I’m looking at the line right here, and it seems it’s just filtering out a specific team. Try replacing it with

if game.Players:GetPlayerFromCharacter(humanoid.Parent).TeamColor ~= vPlayer.TeamColor or game.Players:GetPlayerFromCharacter(humanoid.Parent).Neutral == true then

I changed the first if statement to this ~= vPlayer.TeamColor because currently your code was just filtering out a specific team. If the Team variable was just set to your team then set it to the TeamColor instead. (Also, can you give the code that sets the team variable if this does not work.

That’s the wrong section of code you gave. This was for an example of an Anti-Team Kill sword, completely different script from my weapon, apologies on the confusion.

This is the section of code that has the issue, I just want to know how I can make it so that it prevents, lets say “Red” cannot kill “Red” and “Blue” cannot kill “Blue”

function blow(hit)
	if enabledToDamage == false then return end
	enabledToDamage = false
	if (hit.Parent == nil) then enabledToDamage = true return end -- happens when bullet hits sword
	local humanoid = hit.Parent:findFirstChild("Humanoid")
	local vCharacter = Tool.Parent
	local vPlayer = game.Players:playerFromCharacter(vCharacter)
	local hum = vCharacter:findFirstChild("Humanoid") -- non-nil if tool held by a character
	if humanoid~=nil and humanoid ~= hum and hum ~= nil then
		-- final check, make sure sword is in-hand
		local right_arm = vCharacter:FindFirstChild("Right Arm")
		if (right_arm ~= nil) then
			local joint = right_arm:FindFirstChild("RightGrip")
			if (joint ~= nil and (joint.Part0 == sword or joint.Part1 == sword)) then
				tagHumanoid(humanoid, vPlayer)
				humanoid:TakeDamage(damage)
				wait(1)
				untagHumanoid(humanoid)	
			else
				enabledToDamage = true
			end
		else
			enabledToDamage = true
		end
	else
		enabledToDamage = true
	end
end```

Well do the same thing that I said above. Because it seems your variables are the same.

if game.Players:GetPlayerFromCharacter(humanoid.Parent).TeamColor ~= vPlayer.TeamColor or game.Players:GetPlayerFromCharacter(humanoid.Parent).Neutral == true then
 --put damage code here
end
1 Like

Well then, it worked like a charm. Thanks for the help.

1 Like