Help On Disabling Team Killing

I wrote a sword script, however, I’m really stuck on how I would disable team killing within this script. Does anybody have any idea on how I would incorporate an anti-team killing so that players cannot damage others who are on their team? Any insights or help would be greatly appreciated, thanks alot. The script inside of the sword is below:

local tool = script.Parent
local damage = script.Parent.Damage.Value
local Debris = game:GetService("Debris") 

local function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

local function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

local function onTouch(partOther)
	local humanOther = partOther.Parent:FindFirstChild("Humanoid")
	local player = tool.Parent

	if not humanOther then return end

	TagHumanoid(humanOther, player)

	-- Check if the player being hit is the same team as the tool's parent
	local playerTeam = player:FindFirstChild("Team")
	local otherPlayerTeam = partOther.Parent:FindFirstChild("Team")
	if playerTeam and otherPlayerTeam and playerTeam.Value == otherPlayerTeam.Value then
		return -- Same team, so don't deal damage
	end

	if humanOther.Parent == tool.Parent then return end

	humanOther:TakeDamage(damage)
end

local function slash()
	local str = Instance.new("StringValue")
	str.Name = "toolanim"
	str.Value = "Slash"
	str.Parent = tool
end

tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch)

2 Likes

You’d have to check if the player it’s going to damage is on the same team as the shooter.

local function onTouch(partOther)
    local humanOther = partOther.Parent:FindFirstChild("Humanoid")
    local player = tool.Parent

    if not humanOther then return end

    TagHumanoid(humanOther, player)

    -- Check if the player being hit is the same team as the tool's parent
    local playerTeam = player:FindFirstChild("Team")
    local otherPlayerTeam = partOther.Parent:FindFirstChild("Team")

    -- Check if both players have teams and they are the same
    if playerTeam and otherPlayerTeam and playerTeam.Value == otherPlayerTeam.Value then
        return -- Same team, so don't deal damage
    end

    if humanOther.Parent == tool.Parent then return end

    humanOther:TakeDamage(damage)
end

Here’s the fixed function. I haven’t tested it.

1 Like

Thanks alot, may I ask for a quick summary of what was adjusted in the function?

1 Like

In the onTouch function, the adjustment involved adding a check to determine if the player wielding the sword (tool.Parent) and the player being hit (partOther.Parent) belong to the same team. If they do, damage is prevented from being dealt. This check ensures that team killing is disabled within the script. If the players are not on the same team, damage is dealt as usual.

Doesn’t seem to work, damages player even if they’re on the same team.

There are some errors in your code…

Firstly, you cannot retrieve the player’s team directly from the Workspace.
Secondly, even if the players were on the same team, they would still get killed because the :TakeDamage() function is fired after the check.


When a player holds a tool, the tool’s parent is the player (in the workspace).

local player = tool.Parent

To fix that you should add the Player Service as a variable

local Players = game:GetService("Players")

And then replace:

With:

local playerTeam = Players:FindFirstChild(player.Name).Team
	local otherPlayerTeam = Players:WaitForChild(partOther.Parent.Name).Team
	if playerTeam and otherPlayerTeam and playerTeam == otherPlayerTeam then
		print("Same group")
	else
		print("NOT Same group")
		humanOther:TakeDamage(damage)
	end


Hope this helped you, if you have any other questions let me know! :+1:

2 Likes

Hey there, thanks alot didn’t know there were this much errors.

Question about the code, in the first line local playerTeam = Players:FindFirstChild(player.Name).Team
players doesn’t seem to be defined, do you know what I would replace this with? Would it just be Players?

Nevermind, thanks SO much. This was irritating me for a really long time, glad that someone could help me out.

1 Like

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