Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help With Non-Team Killing Script?

Asked by
Scootakip 299 Moderation Voter
8 years ago
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.

2 answers

Log in to vote
-1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
8 years ago

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)
1
Now the gun doesn't do damage to anything Scootakip 299 — 8y
0
It must be a player for it to damage. You can change that if you want Shawnyg 4330 — 8y
1
It is a Player as well. It won't damage anything, not even players on the other team. Scootakip 299 — 8y
0
@Scootakip Made an edit Shawnyg 4330 — 8y
0
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 Scootakip 299 — 8y
Ad
Log in to vote
-1
Answered by 8 years ago

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.

0
That's kinda what my script is supposed to do.... It doesn't work... This doesn't help. Scootakip 299 — 8y

Answer this question