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

How would I implement an anti-team kill feature into my script?

I have this script for a hand axe that I'm making for a game, so far it works perfectly fine. Each time it hits a humanoid it deals damage. However I also want it so that it wont deal any damage to any of the player's team mates, and I have tried using GetPlayerFromCharacter however I am having trouble on figuring out how to use it in my code when the script detects that a humanoid has hit the handle.

Here is the snippet of my code that fixates on hit detection.

local canDamage = true
canChop = true
Tool.Handle.Touched:connect(function(hit)
	if not swinging then return end
	if not canDamage or not canChop then return end
	
	function damage()
		canDamage = false
		hit.Parent:FindFirstChild("Humanoid"):TakeDamage(26)
		Tool.Handle.FleshHit:Play()
		wait(coolDown2)
		canDamage = true
	end
	
	local function chop()
		canChop = false
		local chopValue = hit.Parent.ChopValue
		local addValue = 1
		chopValue.Value = chopValue.Value - addValue
		Tool.Handle.Chop:Play()
		wait(coolDown2)
		canChop = true
	end
	
	if hit and hit.Parent:FindFirstChild("Humanoid") then
		
		damage()
		
	elseif hit and hit.Parent:FindFirstChild("ChopValue") then --if hit tree then
		
		chop()
		
	end
end)


2 comments

0

No need to do "var == false", or "var == true", you can use "var" and "not var".

0

thanks made some few minor edits to the code that i posted

2 answers

✓ Accepted answer

Alright, I'm going to give you the code for it. So, you'll need to check TeamColors when it does damage. We can do this using GetPlayerFromCharacter. Before the Humanoid takes damage, you'll want to check the player's TeamColor.

 function damage()
        canDamage = false
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local user = game.Players:GetPlayerFromCharacter(Tool.Parent)
		if player then
			if player.TeamColor == user.TeamColor then
				--penalties for teamkilling
			else
				hit.Parent.Humanoid:TakeDamage(26)
			end
		end
	end
        Tool.Handle.FleshHit:Play()
        wait(coolDown2)
        canDamage = true
    end

Before your damage script you can check if the person you are hitting is the same TeamColor or not, if they are then make damage 0, if not make the axe do 26 damage.

3 comments

0

Good job explaining.. bad job providing what you explain in code.

0

Maybe he could have elaborated more, but I'm not sure the dude should've had to code for him.

0

I'm not going to code for him.

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