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

How can I prevent team killing?

Asked by
AZDev 590 Moderation Voter
8 years ago

I created a gun script but it allows team killing how can I edit it to remove this possibility?

Here is my code:

--player variables
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

--gun variables
local gun = script.Parent 
local ammo = 100
local magammo = ammo
local reloading = false
local auto = false -- set to false if you do not want the gun to be automatic
local firing = false
local CanChangeFireModes = false
local damage = 80, 100
--^^If set to true the player can press v to change firemodes from auto to semi and semi to auto

local debounce = false

if not game.workspace:FindFirstChild("Bullet") then
    local bullet_storage = Instance.new("Folder", workspace)
    bullet_storage.Name = "Bullet"
else
    --nothing
end

function getBullet()
    print(ammo)
    if ammo > 0 then
        if debounce == false then
            debounce = true
            ammo = ammo - 1
            gun.Handle.Fire:Play()
            local mh = mouse.Hit
            local B = Instance.new("Part", workspace.Bullet)
            B.CFrame = gun.Handle.CFrame + (player.Character.Torso.CFrame.lookVector)
            B.CFrame = CFrame.new(B.Position, mh.p)
            B.FormFactor = "Custom"
            B.Size = Vector3.new(0.2, 0.2, 0.2)
            B.BrickColor = BrickColor.Black()
            B.CanCollide = false
            local velo = Instance.new("BodyVelocity", B)
            velo.velocity = B.CFrame.lookVector * 120
            velo.maxForce = Vector3.new(math.huge, math.huge, math.huge)
            B.Touched:connect(function(hit)
                local humanoid = hit.Parent:FindFirstChild("Humanoid")
                if humanoid then
                    humanoid:TakeDamage(damage)
                end
            end)
        wait(0)
        debounce = false
        end
    end
end

function autofire()
    if ammo > 0 then
        while firing == true do
            if firing == true and ammo <= 0 then
                firing = false
            end
            print(ammo)
            wait(0)
            gun.Handle.Fire:Play()
            local mh = mouse.Hit
            local B = Instance.new("Part", workspace.Bullet)
            B.CFrame = gun.Handle.CFrame + (player.Character.Torso.CFrame.lookVector)
            B.CFrame = CFrame.new(B.Position, mh.p)
            B.FormFactor = "Custom"
            B.Size = Vector3.new(0.2, 0.2, 0.2)
            B.BrickColor = BrickColor.Black()
            B.CanCollide = false
            ammo = ammo - 1
            local velo = Instance.new("BodyVelocity", B)
            velo.velocity = B.CFrame.lookVector * 120
            velo.maxForce = Vector3.new(math.huge, math.huge, math.huge)
            B.Touched:connect(function(hit)
                local humanoid = hit.Parent:FindFirstChild("Humanoid")
                if humanoid then
                    humanoid:TakeDamage(damage)
                end
            end)
        wait(0.005)
        end
    end
end

mouse.Button1Down:connect(function()
    if auto == true then
        firing = true
        autofire()
    else
        getBullet()
    end
    if ammo <= 0 then
        print('reloading')
        wait(2)
        ammo = magammo
    end
end)

mouse.Button1Up:connect(function()
    if firing == true then
        firing = false
    end
end)

local uis = game:GetService("UserInputService") -- allows firemode change
uis.InputBegan:connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.V and CanChangeFireModes == true then
        if auto == true then
            auto = false
        else
            auto = true
        end
    end
end)

2 answers

Log in to vote
4
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

Player's TeamColor

You can do a check to see if the player that was hit has the same TeamColor property as the player who shot the gun:

B.Touched:connect(function(hit)
    local playerHit = game.Players:GetPlayerFromCharacter(hit.Parent)
    local humanoid = hit.Parent:FindFirstChild("Humanoid") 
    if playerHit and humanoid and player.TeamColor ~= playerHit.TeamColor then
        humanoid:TakeDamage(damage) 
    end
end)
0
You forgot to identify player as a variable in there.. KingLoneCat 2642 — 8y
1
It's identified at the top of his script, this is just a chunk from the middle BlackJPI 2658 — 8y
0
Oh, you actually read his script... Lol ok. KingLoneCat 2642 — 8y
0
It's literally on line 2... BlackJPI 2658 — 8y
Ad
Log in to vote
-2
Answered by 8 years ago

You could use values as in when a player is assigned a team then a Value is parented under his character and add a value to the gun he uses that identifies what team he is on and it changes every time he changes team and then you could make a touched event with the bullet and make it to where if the bullet touches a part and the guns value which identifies the team that the gun is under and if the character that gets touched by that has the same value inside of him you could make it return end and then if the character that gets touched by it doesn't have that value he would take damage so you won't have to worry about the team players getting hurt because they will have the same value as the gun that the bullet is getting shot out of... It might be a bit complicating but, I hope you understood what I meant and I am sorry if you didn't. You can comment below for any questions or concerns!

0
This is a lot of unecessary work. AZDev 590 — 8y
0
Alright, sorry. KingLoneCat 2642 — 8y

Answer this question