How to make anti teamkill more efficient?

I’ve got my anti teamkill script here, but it’s not very efficient. I made it like this because I have multiple teams that are friendly to each other. I wanted to know if there was a better way of doing this?

local safeTeams = {
	{
		"Chaos Insurgency",
		"Class-D"
	},
	{
		"Ethics Committee",
		"Mobile Task Force | Epsilon-11"
	},
	{
		"Ethics Committee",
		"O5 Council",
	},
	{
		"Ethics Committee",
		"Science Department"
	},
	{
		"Ethics Committee",
		"Security Department"
	},
	{
		"Mobile Task Force | Epsilon-11",
		"O5 Council"
	},
	{
		"Mobile Task Force | Epsilon-11",
		"Science Department"
	},
	{
		"Mobile Task Force | Epsilon-11",
		"Security Department"
	},
	{
		"O5 Council",
		"Science Department"
	},
	{
		"O5 Council",
		"Security Department"
	},
	{
		"Science Department",
		"Security Department"
	}
}

local friendly = false

local target = game.Players:GetPlayerFromCharacter(humanoid.Parent)
for _,tbl in pairs(safeTeams) do
		if table.find(tbl, target.Team.Name) and table.find(tbl, player.Team.Name) then
				friendly = true
			    print("No teamkilling!")
			    break
		end
end

if not friendly then
    humanoid:TakeDamage(7)
end
1 Like

At the final line add

friendly = false

Otherwise friendly will be always true.

So like this?

if not friendly then
	humanoid:TakeDamage(7)
	friendly = false
end
local safeTeams = {
	{
		"Chaos Insurgency",
		"Class-D"
	},
	{
		"Ethics Committee",
		"Mobile Task Force | Epsilon-11"
	},
	{
		"Ethics Committee",
		"O5 Council",
	},
	{
		"Ethics Committee",
		"Science Department"
	},
	{
		"Ethics Committee",
		"Security Department"
	},
	{
		"Mobile Task Force | Epsilon-11",
		"O5 Council"
	},
	{
		"Mobile Task Force | Epsilon-11",
		"Science Department"
	},
	{
		"Mobile Task Force | Epsilon-11",
		"Security Department"
	},
	{
		"O5 Council",
		"Science Department"
	},
	{
		"O5 Council",
		"Security Department"
	},
	{
		"Science Department",
		"Security Department"
	}
}

local friendly = {}

local Players = game:GetService("Players")
local target = game.Players:GetPlayerFromCharacter(humanoid.Parent)
for _,tbl in pairs(safeTeams) do
	if table.find(tbl, target.Team.Name) and table.find(tbl, player.Team.Name) then
		 friendly[Players.UserId] = false
		print("No teamkilling!")
		break
	end
end

if friendly[Players.UserId] == false then
	humanoid:TakeDamage(7)
end

Use this.

1 Like