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 1) No player has the same name as the Humanoid 2) 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.
I see your problem. You're going through every player in the game, and say if I'm in the game with you, and on your team. Once that loop reaches me, the if statement will return false and still hurt me! To prevent that, a single if statement using GetPlayerFromCharacter could be used!
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") local target = game.Players:GetPlayerFromCharacter(Part.Parent) if target then -- Checks if it's a valid player if target.TeamColor ~= Player.TeamColor and target ~= Player then -- Checks if they're not on the same team, and if it's not the own player Humanoid:TakeDamage(5) 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)
Why dont you just make it so, when the bullet hits the person, and they have a humanoid, and their teamcolor ~= the player who shot the bullets teamcolor, then it wouldnt take anything off.