Player = game.Players.LocalPlayer
Gun = script.Parent
FireSound = Gun.ShootSound
LoadSound = Gun.ReloadSound
mouse = Player:GetMouse()
enabled = true
Gun.Ammo.Value = 5
MaxAmmo = 5
function e()
script.Parent.CanReload.Value = true
end
function u()
script.Parent.CanReload.Value = false
end
function Shoot()
function Damage(Part)
local Humanoid = Part.Parent:FindFirstChild("Humanoid")
if Humanoid ~= nil and Humanoid.Parent.Name ~= Player.Name then
for i,d in pairs(game.Players:GetChildren()) do
if d.Name ~= Humanoid.Parent.Name then
Humanoid:TakeDamage(5)
end
if d.Name == Humanoid.Parent.Name then
if d.TeamColor ~= Player.TeamColor then
Humanoid:TakeDamage(5)
end
end
end
end
end
if Gun.Ammo.Value > 0 and enabled == true then
Gun.Ammo.Value = Gun.Ammo.Value - 1
enabled = false
Bullet = Instance.new("Part", game.Workspace)
Gun.GripForward = Vector3.new(-0.975, -0.223, -0.02)
game.Debris:AddItem(Bullet, 2)
Bullet.Shape = "Ball"
Bullet.Size = Vector3.new(0.2,0.2,0.2)
Bullet.TopSurface = "Smooth"
Bullet.BottomSurface = "Smooth"
Bullet.Transparency = 1
Bullet.BrickColor = BrickColor.new("Dark stone grey")
Bullet.CanCollide = false
Bullet.CFrame = Gun.Handle.CFrame
Bullet.CFrame = CFrame.new(Bullet.Position,mouse.Hit.p)
v = Instance.new("BodyVelocity", Bullet)
v.velocity = Bullet.CFrame.lookVector *900
v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
FireSound:Play()
Shell = Instance.new("Part", game.Workspace)
Shell.TopSurface = "Smooth"
Shell.BottomSurface = "Smooth"
Shell.Size = Vector3.new(.1,.1,.1)
game.Debris:AddItem(Shell, 0.8)
Shell.CFrame = Gun.Handle.CFrame *CFrame.new(0, -.5, -.3)
Bullet.Touched:connect(Damage)
Light = Instance.new("PointLight", Gun)
wait(.1)
Light:Destroy()
Gun.GripForward = Vector3.new(-0.975, 0, -0.02)
wait()
enabled = true
end
end
function Reload(key)
key = key:lower()
if key == 'r' then
if enabled == true and Gun.Ammo.Value < MaxAmmo and script.Parent.CanReload.Value == true then
LoadSound:Play()
wait(LoadSound.TimeLength)
Gun.Ammo.Value = MaxAmmo
end
end
end
script.Parent.Equipped:connect(e)
script.Parent.Unequipped:connect(u)
mouse.KeyDown:connect(Reload)
Gun.Activated:connect(Shoot)
That's a script to a gun that I made, look towards the topish for a function called "Damage". Damage tells the bullet to only do damage to a humanoid if either
- No player has the same name as the Humanoid
- The humanoid has a matching player that is the same team as the player using the gun
The problem is that this doesn't work, I can still kill teammates. Am I doing something wrong, am I doing it the wrong way, is there a better way for a non-teamkilling script? Please help.

5 comments
Now the gun doesn't do damage to anything
It must be a player for it to damage. You can change that if you want
It is a Player as well. It won't damage anything, not even players on the other team.
@Scootakip Made an edit
I managed to get a working version of it. I'm going to post another question about it since it's raising a few sorta unrelated issues now, but thanks