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)
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.