How to disable team killing?

I want to disable team killing on my gun and on fists. I have tried multiple free models (yes i do know they can have viruses) none of the seemed to work and I also looked at multiple youtube tutorials and they also did nothing. here are 2 of my scripts

gun script:

local uis = game:GetService("UserInputService")


local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local debounce = false

local ammo = script.Parent:WaitForChild("Ammo")

local fireeffect = script.Parent:WaitForChild("FireEffect")

local reloading = false

local char = player.Character
local hum = char.Humanoid

local reload = script.Parent:WaitForChild("Reload")
local reloadtrack = hum:LoadAnimation(script.Reload)

local idle = script.Parent.LocalScript:WaitForChild("Idle")
local idletrack = hum:LoadAnimation(idle)

local reloadsound = script.Parent:WaitForChild("Reload")

local gui = player.PlayerGui:FindFirstChild("GunGui")
local frame = player.PlayerGui:FindFirstChild("GunGui"):FindFirstChild("Frame")
local gunText = frame:FindFirstChild("Gun")
local ModeText = frame:FindFirstChild("Mode")
local AmmoText = frame:FindFirstChild("Ammo")
local MaxAmmoText = frame:FindFirstChild("MaxAmmo")

script.Parent.Activated:Connect(function()

	
	if debounce == false then
		if ammo.Value > 0 then
			if not reloading then
				fireeffect:FireServer()
				ammo.Value -= 1
				local target = mouse.Target
				AmmoText.Text = tostring(ammo.Value)
				if target then
					if target.Parent:FindFirstChild("Humanoid") then
						script.Parent:WaitForChild("Fire"):FireServer(target)
					end
				end
			end
		elseif ammo.Value <= 0 then
			if not reloading then
				reloadsound:Play()
				reloadtrack:Play()
				reloading = true
				AmmoText.Text = "Reloading..."
				hum.WalkSpeed = 8
				ammo.Value = 9
				task.wait(3)
				reloadtrack:Stop()
				hum.WalkSpeed = 14
				reloading = false
				AmmoText.Text = tostring(ammo.Value)
			end
		end
		
		debounce = true
		task.wait(0.5)
		debounce = false
	end
end)

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R then
		if not reloading then
			reloadsound:Play()
			reloadtrack:Play()
			reloading = true
			AmmoText.Text = "Reloading..."
			hum.WalkSpeed = 8
			ammo.Value = 9
			task.wait(3)
			reloadtrack:Stop()
			hum.WalkSpeed = 14
			reloading = false
			AmmoText.Text = tostring(ammo.Value)
		end
	end
end)


script.Parent.Equipped:Connect(function()
	gui.Enabled = true
	gunText.Text = "M1911"
	ModeText.Text = "Semi"
	AmmoText.Text = tostring(ammo.Value)
	MaxAmmoText.Text = "9"
end)

script.Parent.Unequipped:Connect(function()
	gui.Enabled = false
	gunText.Text = "nil"
	ModeText.Text = "nil"
	AmmoText.Text = "nil"
	MaxAmmoText.Text = "nil"
end)

fists script:

local tool = script.Parent
local player = game.Players.LocalPlayer
local idle = script:WaitForChild("Punching Idle")
local char = player.Character
local hum = char.Humanoid
local idletrack = hum:LoadAnimation(idle)
local rightpunch = script:WaitForChild("Right Punch")
local rpunchtrack = hum:LoadAnimation(rightpunch)
local leftpunch = script:WaitForChild("Left Punch")
local lpunchtrack = hum:LoadAnimation(leftpunch)
local cas = game:GetService("ContextActionService")

local swingSound = script.Parent:FindFirstChild("Swing")
local hitSound = script.Parent:FindFirstChild("Hit")

local rs = game:GetService("ReplicatedStorage")

local event = rs:FindFirstChild("Events")
local hitboxEvent = event:FindFirstChild("Hitbox")

local gui = player.PlayerGui.GunGui
local frame = player.PlayerGui.GunGui.Frame
local gunText = frame:FindFirstChild("Gun")
local ModeText = frame:FindFirstChild("Mode")
local AmmoText = frame:FindFirstChild("Ammo")
local MaxAmmoText = frame:FindFirstChild("MaxAmmo")
local slash = frame:FindFirstChild("Slash")

tool.Equipped:Connect(function()
	
	gui.Enabled = true
	
	gunText.Text = "Fists"
	ModeText.Text = "Lethal"
	AmmoText.Visible = false
	MaxAmmoText.Visible = false
	slash.Visible = false
	
	local currentPunch = 0

	local debounce = false
	
	idletrack:Play()
	
	tool.Activated:Connect(function()
		if debounce then return end

		debounce = true

		if currentPunch == 0 then
			rpunchtrack:Play()
			hitboxEvent:FireServer(Vector3.new(3,3,3), Vector3.new(2), 20, 0.3)
			swingSound:Play()
			task.wait(0.7)
			debounce = false
		elseif currentPunch == 1 then
			lpunchtrack:Play()
			hitboxEvent:FireServer(Vector3.new(3,3,3), Vector3.new(2), 20, 0.3)
			swingSound:Play()
			task.wait(0.7)
			debounce = false
		elseif currentPunch == 2 then
			rpunchtrack:Play()
			hitboxEvent:FireServer(Vector3.new(3,3,3), Vector3.new(2), 20, 0.3)
			swingSound:Play()
			task.wait(0.7)
			debounce = false
		end
		if currentPunch == 2 then
			currentPunch = 0
		else
			currentPunch += 1
		end
	end)
	
	tool.Unequipped:Connect(function()
		gunText.Text = "nil"
		ModeText.Text = "nil"
		AmmoText.Visible = true
		MaxAmmoText.Visible = true
		slash.Visible = true
		
		gui.Enabled = false
		idletrack:Stop()
	end)
end)

so how can i disable team killing in both of these scripts… if you need the server script for both of them then let me know

add an if statement before you deal damage to the players, if the person shooting is on the same team as the target player, then don’t deal any damage.

3 Likes

These 2 are just local scripts, if you want to detect team kill, I would need to see your hit detection/raycast system which im guessing is on the server? So provide the server script please, I also cleaned up your gun script, because it has way to much nested if statements and is just a mess, I would recommend you look into OOP or a if not code structure rather than nested if statements:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local debounce = false

local ammo = script.Parent:WaitForChild("Ammo")
local fireeffect = script.Parent:WaitForChild("FireEffect")

local reloading = false

local char = player.Character
local hum = char.Humanoid

local reload = script.Parent:WaitForChild("Reload")
local reloadtrack = hum:LoadAnimation(script.Reload)

local idle = script.Parent.LocalScript:WaitForChild("Idle")
local idletrack = hum:LoadAnimation(idle)

local reloadsound = script.Parent:WaitForChild("Reload")

local gui = player.PlayerGui:FindFirstChild("GunGui")
local frame = player.PlayerGui:FindFirstChild("GunGui"):FindFirstChild("Frame")
local gunText = frame:FindFirstChild("Gun")
local ModeText = frame:FindFirstChild("Mode")
local AmmoText = frame:FindFirstChild("Ammo")
local MaxAmmoText = frame:FindFirstChild("MaxAmmo")

local Team = game.Teams.Red

script.Parent.Activated:Connect(function()
	
	if debounce then return end

	if not debounce and ammo.Value > 0 then
			if not reloading then
				fireeffect:FireServer()
				ammo.Value -= 1
			local target = mouse.Target
			
			AmmoText.Text = tostring(ammo.Value)
			
			if target then	
				script.Parent:WaitForChild("Fire"):FireServer(target)
			end
			
		elseif ammo.Value <= 0 and not reloading then
				reloadsound:Play()
				reloadtrack:Play()
				reloading = true
				AmmoText.Text = "Reloading..."
				hum.WalkSpeed = 8
				ammo.Value = 9
				task.wait(3)
				reloadtrack:Stop()
				hum.WalkSpeed = 14
				reloading = false
				AmmoText.Text = tostring(ammo.Value)
		end

		debounce = true
		task.wait(0.5)
		debounce = false
	end
end)

uis.InputBegan:Connect(function(input, gameProccessed)
	
	if gameProccessed then return end
	
	if input.KeyCode == Enum.KeyCode.R and not reloading then
			reloadsound:Play()
			reloadtrack:Play()
			reloading = true
			AmmoText.Text = "Reloading..."
			hum.WalkSpeed = 8
			ammo.Value = 9
			task.wait(3)
			reloadtrack:Stop()
			hum.WalkSpeed = 14
			reloading = false
			AmmoText.Text = tostring(ammo.Value)
	end
end)


script.Parent.Equipped:Connect(function()
	gui.Enabled = true
	gunText.Text = "M1911"
	ModeText.Text = "Semi"
	AmmoText.Text = tostring(ammo.Value)
	MaxAmmoText.Text = "9"
end)

script.Parent.Unequipped:Connect(function()
	gui.Enabled = false
	gunText.Text = "nil"
	ModeText.Text = "nil"
	AmmoText.Text = "nil"
	MaxAmmoText.Text = "nil"
end)

This is the way, look for the part that deals damage (try ctrl+shift+f :TakeDamage ) and enclose it in an if statement that only deals damage if the teams are different

here is the server script for the gun:

script.Parent:WaitForChild("Fire").OnServerEvent:Connect(function(player, target, Gun)
	target.Parent.Humanoid.Health -= 15
end)




script.Parent:WaitForChild("FireEffect").OnServerEvent:Connect(function()
	script.Parent.FireSound:Play()
	script.Parent.Part.Muzzle.Enabled = true
	script.Parent.Part.Muzzle.Enabled = true
	script.Parent.Part.Muzzle.Enabled = true
	script.Parent.Part["Muzzle Flash"].Enabled = true
	task.wait(0.1)
	script.Parent.Part.Muzzle.Enabled = false
	script.Parent.Part.Muzzle.Enabled = false
	script.Parent.Part.Muzzle.Enabled = false
	script.Parent.Part["Muzzle Flash"].Enabled = false
end)


and here is the server script for the fists:

local rs = game:GetService("ReplicatedStorage")

local event = rs:FindFirstChild("Events")
local hitboxEvent = event:FindFirstChild("Hitbox")
local swingSound = game:GetService("StarterPack"):FindFirstChild("Fists"):FindFirstChild("Swing")
local hitSound = game:GetService("StarterPack"):FindFirstChild("Fists"):FindFirstChild("Hit")

local function newHitbox(character, size, offest, damage, linger)
	local hrp = character:FindFirstChild("HumanoidRootPart")
	if hrp == nil then return end
	local weld = Instance.new("WeldConstraint", hrp)
	local hitbox = Instance.new("Part")
	weld.Part0 = hrp
	weld.Part1 = hitbox
	
	hitbox.Transparency = 1
	hitbox.CanCollide = false
	hitbox.CanQuery = false
	hitbox.Massless = true
	
	hitbox.Size = size
	hitbox.CFrame = hrp.CFrame + hrp.CFrame.LookVector * offest.X + Vector3.new(0, offest.Y)
	hitbox.Parent = character
	
	hitbox.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") == nil then return end
		
		for _, v in pairs(hitbox:GetChildren()) do
			if v:IsA("ObjectValue") then
				if v.Value == hit.Parent then return end
			end
		end
		
		local hitCounter = Instance.new("ObjectValue", hitbox)
		hitCounter.Value = hit.Parent
		
		hitSound:Play()
		
		hit.Parent.Humanoid:TakeDamage(damage)
	end)
	
	game.Debris:addItem(hitbox, linger)
end

hitboxEvent.OnServerEvent:Connect(function(plr, size, offest, damage, linger)
	newHitbox(plr.Character, size, offest, damage, linger)
end)

here’s an example so you could maybe get the idea like a sword:

local myplr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)

script.Parent.Touched:Connect(function(hit)
local char = hit.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
if plr then
if plr.Team == myplr.Team then
return
end
end
–deal damage to the humanoid or something
end)

return cancels the touch event btw

Gun server script



script.Parent:WaitForChild("Fire").OnServerEvent:Connect(function(player, target, Gun)
    local attackerPlayer = game.Players:GetPlayerFromCharacter(player.Character)
    local targetPlayer = game.Players:GetPlayerFromCharacter(target.Parent)
    
    if attackerPlayer and targetPlayer and attackerPlayer.Team ~= targetPlayer.Team then
        if target.Parent:FindFirstChild("Humanoid") then
            target.Parent.Humanoid:TakeDamage(15)
        end
    end
end)


script.Parent:WaitForChild("FireEffect").OnServerEvent:Connect(function()
    local part = script.Parent.Part
    local muzzle = part:FindFirstChild("Muzzle")
    local muzzleFlash = part:FindFirstChild("Muzzle Flash")
    local fireSound = script.Parent:FindFirstChild("FireSound")

    if fireSound then
        fireSound:Play()
    end

    if muzzle then
        muzzle.Enabled = true
    end

    if muzzleFlash then
        muzzleFlash.Enabled = true
    end

    task.wait(0.1)

    if muzzle then
        muzzle.Enabled = false
    end

    if muzzleFlash then
        muzzleFlash.Enabled = false
    end
end)
 

Melee server script


local rs = game:GetService("ReplicatedStorage")
local event = rs:FindFirstChild("Events")
local hitboxEvent = event:FindFirstChild("Hitbox")
local swingSound = game:GetService("StarterPack"):FindFirstChild("Fists"):FindFirstChild("Swing")
local hitSound = game:GetService("StarterPack"):FindFirstChild("Fists"):FindFirstChild("Hit")

local function newHitbox(character, size, offset, damage, linger)
    local hrp = character:FindFirstChild("HumanoidRootPart")
    if hrp == nil then return end

    local weld = Instance.new("WeldConstraint", hrp)
    local hitbox = Instance.new("Part")
    weld.Part0 = hrp
    weld.Part1 = hitbox

    hitbox.Transparency = 1
    hitbox.CanCollide = false
    hitbox.CanQuery = false
    hitbox.Massless = true

    hitbox.Size = size
    hitbox.CFrame = hrp.CFrame + hrp.CFrame.LookVector * offset.X + Vector3.new(0, offset.Y)
    hitbox.Parent = character

    hitbox.Touched:Connect(function(hit)
        local hitParent = hit.Parent
        if hitParent:FindFirstChild("Humanoid") == nil then return end

        for _, v in pairs(hitbox:GetChildren()) do
            if v:IsA("ObjectValue") and v.Value == hitParent then
                return
            end
        end
        
        local attackerPlayer = game.Players:GetPlayerFromCharacter(character)
        local targetPlayer = game.Players:GetPlayerFromCharacter(hitParent)
        if attackerPlayer and targetPlayer and attackerPlayer.Team == targetPlayer.Team then
            return 
        end

        local hitCounter = Instance.new("ObjectValue", hitbox)
        hitCounter.Value = hitParent

        hitSound:Play()

        hitParent.Humanoid:TakeDamage(damage)
    end)

    game.Debris:AddItem(hitbox, linger)
end

hitboxEvent.OnServerEvent:Connect(function(plr, size, offset, damage, linger)
    newHitbox(plr.Character, size, offset, damage, linger)
end)

Replace the code with the updated versions and u should be good

it worked thank you so much for your help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.