Help with Anti-Team Kill [MILSIM PROJECT]

So today, i’m making a MILSIM game and i decided to take one of the gun systems in the toolbox but there is a problem, it team kills. My game is based off REALISTIC WAR by FractalReality, except with a different concept and different backstory.

Why cant i do it myself ?
I am still learning scripting, 3 months of learning LUA still doesn’t understand about Anti-Team kill codes. So thats why i look into free model codes and try understanding them

Please reply if you know, scripts are down below

ServerMain :

local maxammo = script.Parent.MaxAmmo.Value
local ammo = script.Parent.Ammo
local reloading = false









local spread = script.Parent.EDIT.Spread.Value






script.Parent.Shoot.OnServerEvent:Connect(function (plr,Target)
	if plr.Character ~= nil and plr.Character.Humanoid.Health >= .1 and script.Parent.Enabled == true and reloading == false then
		script.Parent.Enabled = false
		local hit = false

		if ammo.Value >= 1 then
			ammo.Value = ammo.Value - 1
			script.Parent.Handle.FireSound:Play()
			for i = 1, script.Parent.EDIT.BulletsPerShot.Value do
				local dist = (script.Parent.RPart.Position - Target).Magnitude
				local ray = Ray.new(script.Parent.RPart.CFrame.p, (Target - script.Parent.RPart.CFrame.p + (Vector3.new(math.random(-spread,spread)/1000,math.random(-spread,spread)/1000,math.random(-spread,spread)/1000) * dist)).unit * 2000)
				local part, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
				print(position)
				print(part)
				print(Target)
				local beam = Instance.new("Part")
				beam.Anchored = true
				beam.CanCollide = false
				beam.Material = "Neon"
				if dist >= 60 then
					beam.Size = Vector3.new(.05,.05,60)
				else
					beam.Size = Vector3.new(.05,.05,dist)
				end
				beam.CFrame = CFrame.new(script.Parent.RPart.CFrame.p, position) * CFrame.new(0,0,-beam.Size.Z/2)
				game.Debris:AddItem(beam,10)
				local fx = script.Parent.FX:Clone()
				fx.Dist.Value = dist
				fx.Parent = beam
				fx.Disabled = false
				beam.Parent = plr.Character

				script.Parent.RPart.MuzzleFlashMain.Enabled = true
				script.Parent.RPart.OverHeat.Enabled = true
				script.Parent.RPart.Smoke.Enabled = true
				script.Parent.RPart.Sparks.Enabled = true
				script.Parent.RPart.Flash.Enabled = true
				if part ~= nil then
					if part.Parent:FindFirstChild("Humanoid") then
						part.Parent:FindFirstChild("Humanoid"):TakeDamage(script.Parent.EDIT.Damage.Value)
						hit = true
						local blood = script.Parent.EDIT.Blood:Clone()
						blood.Parent = part
						blood.Enabled = true
						game.Debris:AddItem(blood,.2)
					elseif part.Parent.Parent:FindFirstChild("Humanoid") then
						part.Parent.Parent:FindFirstChild("Humanoid"):TakeDamage(script.Parent.EDIT.Damage.Value)
						hit = true
						local blood = script.Parent.EDIT.Blood:Clone()
						blood.Parent = part
						blood.Enabled = true
						game.Debris:AddItem(blood,.2)
					end
				end
				local hole = Instance.new("Part")
				hole.Size = Vector3.new(.2,.2,.2)
				hole.Material = "Neon"
				hole.Anchored = true
				hole.BrickColor = BrickColor.new("Black")
				hole.Shape = 0
				hole.CanCollide = false
				hole.Position = position
				local dust = script.Parent.EDIT.Dust:Clone()
				dust.Parent = hole
				dust.Enabled = true
				dust.Script.Disabled = false
				game.Debris:AddItem(hole, 1)
				if hit == false then
					hole.Parent = workspace
				end
			end
			local shell = Instance.new("Part")
			shell.Size = Vector3.new(.5,.5,.1)
			shell.Position = script.Parent.ShellPart.Position
			shell.Orientation = script.Parent.ShellPart.Orientation
			shell.CanCollide = false
			local shellmesh = Instance.new("SpecialMesh",shell)
			shellmesh.MeshId = "rbxassetid://94295100"
			shellmesh.TextureId = "rbxassetid://94287792"
			shellmesh.Scale = Vector3.new(3,3,1)
			game.Debris:AddItem(shell,5)
			local v = script.Parent.ShellPart.CFrame:vectorToWorldSpace(Vector3.new(2 + math.random(4,14),math.random(-2,2),math.random(-2,2)))
			shell.Velocity = v
			shell.RotVelocity = Vector3.new(math.random(-25,25)/12,math.random(-25,25)/12,math.random(-25,25)/12)
			shell.Parent = plr.Character
			wait()
			script.Parent.RPart.MuzzleFlashMain.Enabled = false
			script.Parent.RPart.OverHeat.Enabled = false
			script.Parent.RPart.Smoke.Enabled = false
			script.Parent.RPart.Sparks.Enabled = false
			script.Parent.RPart.Flash.Enabled = false
		elseif ammo.Value <= 0 then
			script.Parent.Handle.NoAmmo:Play()
		end
		wait(script.Parent.EDIT.Firerate.Value)

		script.Parent.Enabled = true
	end
end)
script.Parent.Reload.OnServerEvent:Connect(function ()
	if reloading == false then
		reloading = true
		script.Parent.Handle.Reload:Play()
		wait(script.Parent.EDIT.ReloadTime.Value)
		ammo.Value = maxammo
		reloading = false
	end
end)

ClientMain :

local held = false
local plr = game.Players.LocalPlayer
script.Parent.Equipped:Connect(function()
	chr = script.Parent.Parent
end)

script.Parent.Activated:Connect(function ()
	held = true
	if script.Parent.Enabled == true then
		script.Parent.Enabled = false


		while held == true do
			if script.Parent.EDIT.BurstShots.Value >= 2 then
				for i = 1, script.Parent.EDIT.BurstShots.Value do
					script.Parent.Shoot:FireServer(chr.Humanoid.TargetPoint)
					fire:Play()
					wait(script.Parent.EDIT.BurstRate.Value)


				end
			else
				fire:Play()
				script.Parent.Shoot:FireServer(chr.Humanoid.TargetPoint)
			end


			if script.Parent.EDIT.Auto.Value == false then
				held = false
			end
			wait(script.Parent.EDIT.Firerate.Value + .1)
		end
		if script.Parent.EDIT.Auto.Value == false then
			wait(script.Parent.EDIT.Firerate.Value + .1)
		end
		script.Parent.Enabled = true


	end
end)

script.Parent.Deactivated:Connect(function()
	held = false

end)
script.Parent.Equipped:Connect(function()
	wait()
	holdanim = chr.Humanoid:LoadAnimation(script.Parent.HoldAnim)
	holdanim:Play()
	rel = chr.Humanoid:LoadAnimation(script.Parent.ReloadAnim)
	fire = chr.Humanoid:LoadAnimation(script.Parent.FireAnim)
	gui = script.Ammo:Clone()
	gui.Txt.Text = script.Parent.Ammo.Value
	gui.Parent = plr.PlayerGui
	on = true
end)
script.Parent.Unequipped:Connect(function()
	held = false
	holdanim:Stop()
	fire:Stop()
	rel:Stop()
	gui:Destroy()
	on = false
end)
game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(KeyPressed)
	if KeyPressed == "r" and script.Parent.Parent == plr.Character then 
		script.Parent.Reload:FireServer()

		rel:Play()
	end
end)

script.Parent:WaitForChild("Ammo").Changed:Connect(function ()
	if on == true then
		gui.Txt.Text = script.Parent.Ammo.Value
	end
end)

Let me repeat myself, it is a free model and im planning on remaking the gun system soon. The free model is just for the Early Access of the game.

This script is messy. We need more details. To make an Anti-Team kill you need to get a weapon. I’ll make a sword for you that doesn’t damage/kill your partener.

1 Like

Thats what i was thinking, the scripts are pretty messy and sort of weird. But it functions pretty well

1 Like

Here is a knife for you.
I didn’t test my weapon after I added the anti team kill so I would like a feedback about it.

1 Like

this is where damage is handled:

if part.Parent:FindFirstChild("Humanoid") then
						part.Parent:FindFirstChild("Humanoid"):TakeDamage(script.Parent.EDIT.Damage.Value)
						hit = true
						local blood = script.Parent.EDIT.Blood:Clone()
						blood.Parent = part
						blood.Enabled = true
						game.Debris:AddItem(blood,.2)
					elseif part.Parent.Parent:FindFirstChild("Humanoid") then
						part.Parent.Parent:FindFirstChild("Humanoid"):TakeDamage(script.Parent.EDIT.Damage.Value)
						hit = true
						local blood = script.Parent.EDIT.Blood:Clone()
						blood.Parent = part
						blood.Enabled = true
						game.Debris:AddItem(blood,.2)
					end

just make conditions to check if same team, return, else damage.

1 Like

You cannot expect me to do the anti team kill if i dont know what is the ancestor of the script. @CaIIy123456 ,give me a screenshot of the tool its descendants.

??? im talking to OP, that code snippet is in her server code

plus you can just use findall and type in code to find it within all scripts

Okay, my bad but this is exactly where she needs help. I dont think she knows how to make the conditions to check if the the tool owner and the target are in the same team or else she wouldn’t need help about this.

If there are some tutorials, would you mind sending them here ? or could you tell me about the line of code for the Anti-Team Kill. Possibly, i could take some line of code from “Working Gun” since it has Anti-Team Kill coded in.

But still, it is pretty hard scripting a gun. But i possibly have a more simple gun with a simple code than the Messy code, but it doesnt deal any damage to the enemy. So could you inspect the code ? I made it from most tutorials on YT.

Damage :

local re = script.Parent:WaitForChild("RemoteEvent")
local damage = 20
local bang = script.Parent.Handle.FireSound
local VeryFar = script.Parent.Handle.VeryFar
local MuzzleFlashMain = script.Parent.Handle.MuzzlePoint.MuzzleFlashMain
local Smoke = script.Parent.Handle.MuzzlePoint.Smoke
local Bolt = script.Parent.Bolt
local BoltBack = script.Parent.BoltBack
local Players = game:GetService("Players")

local function onShoot(player, target)
	bang:Play()
	VeryFar:Play()
	MuzzleFlashMain.Enabled = true
	Smoke.Enabled = true
	Bolt.Transparency = 1
	BoltBack.Transparency = 0
	wait(0.2)
	MuzzleFlashMain.Enabled = false
	Smoke.Enabled = false
	Bolt.Transparency = 0
	BoltBack.Transparency = 1
	if target and target.Parent then
		local hum =  target.Parent:FindFirstChild("Humanoid")
		if hum and Players:GetPlayerFromCharacter(target.Parent).Team ~= player.Team then
			hum:TakeDamage(damage)
		end
	end
end

re.OnServerEvent:Connect(onShoot)

LocalHandler :

local gun =  script.Parent
local re = script.Parent:WaitForChild("RemoteEvent")
local player = nil
local char = nil
local mouse =  nil
local connection =  nil
local CanShoot = true
local Enabled = true
local FireRate = 0.2

local function onActivated()
	local camera = game.Workspace.CurrentCamera
	if not Enabled then return end
	Enabled = false
	re:FireServer(mouse.Target)
	camera.CFrame = camera.CFrame * CFrame.Angles(0.03,math.rad(0),0)
	wait(FireRate)
	Enabled = true
end

local function onEquipped()
	player = game.Players.LocalPlayer
	mouse = player:GetMouse()
	connection = gun.Activated:Connect(onActivated)
end

local function onUnequipped()
	player = nil
	mouse = nil
	connection:Disconnect()
end

gun.Equipped:Connect(onEquipped)
gun.Unequipped:Connect(onUnequipped)

The reason i stopped using the simple gun is because it doesnt deal any damage to the enemy somehow

I would like to give you a line of code but I need a screen of the tool and its descendants.

Heres the simple gun descendants
image

And heres the One with the messy code
image

I think @Juicy_Fruit found how to handle this, just adding conditions where the damage is handled.

Basic example:
(just replace the old block with something like this)

-- HANDLING DAMAGE OR NOT
if part ~= nil then
	if part.Parent:FindFirstChild("Humanoid") then
		-- GET THE PLAYER INSTANCE
		local targetPlayer = game.Players:GetPlayerFromCharacter(part.Parent)
		-- COMPARE IF PLAYER BELONGS TEAM OR NOT
		if plr.Team ~= targetPlayer.Team then
			-- DEAL THE DAMAGE
			part.Parent:FindFirstChild("Humanoid"):TakeDamage(script.Parent.EDIT.Damage.Value)
		end

		hit = true
		local blood = script.Parent.EDIT.Blood:Clone()
		blood.Parent = part
		blood.Enabled = true
		game.Debris:AddItem(blood,.2)

	elseif part.Parent.Parent:FindFirstChild("Humanoid") then
		-- GET THE PLAYER INSTANCE
		local targetPlayer = game.Players:GetPlayerFromCharacter(part.Parent)
		-- COMPARE IF PLAYER BELONGS TEAM OR NOT
		if plr.Team ~= targetPlayer.Team then
			-- DEAL THE DAMAGE
			part.Parent.Parent:FindFirstChild("Humanoid"):TakeDamage(script.Parent.EDIT.Damage.Value)
		end

		hit = true
		local blood = script.Parent.EDIT.Blood:Clone()
		blood.Parent = part
		blood.Enabled = true
		game.Debris:AddItem(blood,.2)
	end
end

I think the code is messy yup, and its hard to accomplish it when you are fusing or using code from tutorials.

The easy way is to “fix” the current code and make it to work, but, its important to actually learn how achieve a gun, no matter if your first weapons are not advanced, its important to learn how weaps should work, step by step and no rushing. Maybe build your own gun no matter if its basic.

The “server main code” maybe should look like this after adding the team conditions, I dont know if you are using the default teams from roblox, or custom ones, or how your weapons works, but this should be a very basic way to decide what player to damage or not: (code not tested so expect any error)

local maxammo = script.Parent.MaxAmmo.Value
local ammo = script.Parent.Ammo
local reloading = false
local spread = script.Parent.EDIT.Spread.Value

script.Parent.Shoot.OnServerEvent:Connect(function (plr,Target)
	if plr.Character ~= nil and plr.Character.Humanoid.Health >= .1 and script.Parent.Enabled == true and reloading == false then
		script.Parent.Enabled = false
		local hit = false

		if ammo.Value >= 1 then
			ammo.Value = ammo.Value - 1
			script.Parent.Handle.FireSound:Play()
			for i = 1, script.Parent.EDIT.BulletsPerShot.Value do
				local dist = (script.Parent.RPart.Position - Target).Magnitude
				local ray = Ray.new(script.Parent.RPart.CFrame.p, (Target - script.Parent.RPart.CFrame.p + (Vector3.new(math.random(-spread,spread)/1000,math.random(-spread,spread)/1000,math.random(-spread,spread)/1000) * dist)).unit * 2000)
				local part, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
				print(position)
				print(part)
				print(Target)
				local beam = Instance.new("Part")
				beam.Anchored = true
				beam.CanCollide = false
				beam.Material = "Neon"
				if dist >= 60 then
					beam.Size = Vector3.new(.05,.05,60)
				else
					beam.Size = Vector3.new(.05,.05,dist)
				end
				beam.CFrame = CFrame.new(script.Parent.RPart.CFrame.p, position) * CFrame.new(0,0,-beam.Size.Z/2)
				game.Debris:AddItem(beam,10)
				local fx = script.Parent.FX:Clone()
				fx.Dist.Value = dist
				fx.Parent = beam
				fx.Disabled = false
				beam.Parent = plr.Character

				script.Parent.RPart.MuzzleFlashMain.Enabled = true
				script.Parent.RPart.OverHeat.Enabled = true
				script.Parent.RPart.Smoke.Enabled = true
				script.Parent.RPart.Sparks.Enabled = true
				script.Parent.RPart.Flash.Enabled = true

				-- HANDLING DAMAGE OR NOT
				if part ~= nil then
					if part.Parent:FindFirstChild("Humanoid") then
						-- GET THE PLAYER INSTANCE
						local targetPlayer = game.Players:GetPlayerFromCharacter(part.Parent)
						-- COMPARE IF PLAYER BELONGS TEAM OR NOT
						if plr.Team ~= targetPlayer.Team then
							-- DEAL THE DAMAGE
							part.Parent:FindFirstChild("Humanoid"):TakeDamage(script.Parent.EDIT.Damage.Value)
						end

						hit = true
						local blood = script.Parent.EDIT.Blood:Clone()
						blood.Parent = part
						blood.Enabled = true
						game.Debris:AddItem(blood,.2)

					elseif part.Parent.Parent:FindFirstChild("Humanoid") then
						-- GET THE PLAYER INSTANCE
						local targetPlayer = game.Players:GetPlayerFromCharacter(part.Parent)
						-- COMPARE IF PLAYER BELONGS TEAM OR NOT
						if plr.Team ~= targetPlayer.Team then
							-- DEAL THE DAMAGE
							part.Parent.Parent:FindFirstChild("Humanoid"):TakeDamage(script.Parent.EDIT.Damage.Value)
						end

						hit = true
						local blood = script.Parent.EDIT.Blood:Clone()
						blood.Parent = part
						blood.Enabled = true
						game.Debris:AddItem(blood,.2)
					end
				end

				local hole = Instance.new("Part")
				hole.Size = Vector3.new(.2,.2,.2)
				hole.Material = "Neon"
				hole.Anchored = true
				hole.BrickColor = BrickColor.new("Black")
				hole.Shape = 0
				hole.CanCollide = false
				hole.Position = position
				local dust = script.Parent.EDIT.Dust:Clone()
				dust.Parent = hole
				dust.Enabled = true
				dust.Script.Disabled = false
				game.Debris:AddItem(hole, 1)
				if hit == false then
					hole.Parent = workspace
				end
			end
			local shell = Instance.new("Part")
			shell.Size = Vector3.new(.5,.5,.1)
			shell.Position = script.Parent.ShellPart.Position
			shell.Orientation = script.Parent.ShellPart.Orientation
			shell.CanCollide = false
			local shellmesh = Instance.new("SpecialMesh",shell)
			shellmesh.MeshId = "rbxassetid://94295100"
			shellmesh.TextureId = "rbxassetid://94287792"
			shellmesh.Scale = Vector3.new(3,3,1)
			game.Debris:AddItem(shell,5)
			local v = script.Parent.ShellPart.CFrame:vectorToWorldSpace(Vector3.new(2 + math.random(4,14),math.random(-2,2),math.random(-2,2)))
			shell.Velocity = v
			shell.RotVelocity = Vector3.new(math.random(-25,25)/12,math.random(-25,25)/12,math.random(-25,25)/12)
			shell.Parent = plr.Character
			wait()
			script.Parent.RPart.MuzzleFlashMain.Enabled = false
			script.Parent.RPart.OverHeat.Enabled = false
			script.Parent.RPart.Smoke.Enabled = false
			script.Parent.RPart.Sparks.Enabled = false
			script.Parent.RPart.Flash.Enabled = false
		elseif ammo.Value <= 0 then
			script.Parent.Handle.NoAmmo:Play()
		end
		wait(script.Parent.EDIT.Firerate.Value)

		script.Parent.Enabled = true
	end
end)
script.Parent.Reload.OnServerEvent:Connect(function ()
	if reloading == false then
		reloading = true
		script.Parent.Handle.Reload:Play()
		wait(script.Parent.EDIT.ReloadTime.Value)
		ammo.Value = maxammo
		reloading = false
	end
end)

If you want a quick solution, would be adding conditions, and maybe creating your own weapon to deal damage and avoid team members, plus animations, sound, etc would be fun to learn

I’ll try it out, i’ve made my simple gun to an advanced gun just by editing it.

1 Like

It seems like the code broke the gun, i’ll be sticking with the simple gun instead. But thank you for your response

I suggest making your own guns, yup.
But fixing the script is not impossible, what is the error or unexpected behaviour?

Hi, sorry for the late response.

You could try changing the first line of your if part ~= nil then statement from if part.Parent:FindFirstChild("Humanoid") then to if part.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(part.Parent).Team ~= plr.Team then

Basically the same thing the others suggested without modifying any of the other code.
Let me know if that helps.