Hi there, I am trying to make an Anti-TeamKill script. I am using the PaintballGun as a template for that. The problem is that instead of not dealing damage to teammates, it does not deal damage to everyone. The code is pasted below:
ball = script.Parent damage = 20 teamD = 0 connection = ball.Touched:connect(onTouched) function onTouched(hit) if hit.Name == "Bullet" then return end local humanoid = hit.Parent:FindFirstChild("Humanoid") connection:disconnect() if humanoid ~= nil then tagHumanoid(humanoid) local target = game.Players:GetPlayerFromCharacter(hit.Parent) if hit.Parent ~= nil then if game.Players:GetPlayerFromCharacter(hit.Parent.Name) ~= nil then if target.TeamColor == script.Parent.creator.Value.TeamColor then humanoid:TakeDamage(teamD) elseif target.TeamColor ~= script.Parent.creator.Value.TeamColor then humanoid:TakeDamage(damage) end end end if hit.Name == "Head" then damage = damage * 3 end humanoid:TakeDamage(damage) else end ball:remove() end function tagHumanoid(humanoid) -- todo: make tag expire local tag = ball:findFirstChild("creator") if tag ~= nil then local new_tag = tag:clone() new_tag.Parent = humanoid game.Debris:AddItem(new_tag, 1) end end wait(5) ball:destroy() end wait(5) ball:remove()
The script runs a function which test whether the target's TeamColor is the TeamColor of the shooter (creator). But instead of doing that it does not deal damage at all. If you find any problem please let me know what errors I have made. Thank you for reading :)
One, you connect .Touched before you define the function.
Two, you disconnect the connection before you are even sure it hit a player.
Three, the value of the ball's original tag might be incorrect. Also, you tag hit humanoids for seemingly no reason.
Four, you try game.Players:GetPlayerFromCharacter(hit.Parent.Name)
. The parameter is supposed to be a character.
BTW: One, don't use :remove() unless you need to restore the ball. Use :Destroy(). Two, you can simply say if var then
rather than if var~=nil then
. Three, you should compare Teams rather than TeamColors.