Help with Anti Team Kill

So today i scripted a simple gun system but i have an issue with it, i am making it for a Survival game and Teamwork is needed for the game except my gun damages players in the same team. I need a solution for this since the game is inspired by The Walking Dead, if you needed to see the code it’s down below

Handler :

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 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 then
hum:TakeDamage(damage)
end
end
end
re.OnServerEvent:Connect(onShoot)

Note :
I just learned scripting like 3 months ago and i mostly follow tutorials in order to make this gun system, there is a local Handler so if you need to see it aswell, just ask me.

1 Like

Change:

if hum then
hum:TakeDamage(damage)
end

To:

if hum and Players:GetPlayerFromCharacter(target.Parent).Team ~= player.Team then
hum:TakeDamage(damage)
end

Also add this to the top:

local Players = game:GetService("Players")

Try this out:

local PS = game:GetService("Players")

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 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
	
	local targetChar: Model = target and target:FindFirstAncestorWhichIsA("Model")
	local targetHum: Humanoid = targetChar and targetChar:FindFirstChildWhichIsA("Humanoid")
	local targetPlayer: Player = targetChar and PS:GetPlayerFromCharacter(targetChar)
	
	if targetHum and targetChar and player.Team == targetPlayer.Team then
		targetHum:TakeDamage(damage)
	end
end
re.OnServerEvent:Connect(onShoot)
1 Like