Pls help me to edit this script

I want to write the condition of not harming teammates in my sword script, but I don’t know how to mix the two. Please someone teach me.

local tool = script.Parent

local canDamage = false
local isSlashing = false  -- این متغیر جدید برای جلوگیری از کلیک‌های اتوماتیک اضافه می‌شود

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")

	if not humanoid then 
		return
	end

	if humanoid.Parent ~= tool.Parent and canDamage then 
		humanoid:TakeDamage(5)
	else
		return
	end

	canDamage = false
end

local function slash()
	if isSlashing then  -- اگر در حال حاضر ضربه زده شده باشد، هیچ اقدامی نکن
		return
	end

	isSlashing = true  -- زمانی که ضربه زده می‌شود، به حالت در حال ضربه می‌رود

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

	canDamage = true

	wait(0.5)  -- مدت زمان تاخیر برای جلوگیری از کلیک‌های اتوماتیک
	isSlashing = false  -- بعد از تاخیر، به حالت آماده برای ضربه جدید باز می‌گردد
end

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

this is anti player team damge

if plr.Team ~= target.Team then
    targetHumanoid:TakeDamage(damage)
end
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

local Hostile:{[Team]:{[Team]:true}} = {
	[Teams.TeamHere] = {
		[Teams.EnemyTeam] = true
	}

}

local function IsAlly(team:Team,otherTeam:Team):boolean
	return Hostile[team][otherTeam] or false
end

local tool = script.Parent
local Plr_ToolHolder--- define player who is holding the tool

local canDamage = false
local isSlashing = false  -- این متغیر جدید برای جلوگیری از کلیک‌های اتوماتیک اضافه می‌شود

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")::Humanoid?
	if not humanoid then return end
	local char = humanoid.Parent::Model
	local plr = Players:GetPlayerFromCharacter(char)::Player?
	
	if not plr or not IsAlly(Plr_ToolHolder,plr.Team) then 
		humanoid:TakeDamage(5)
	else
		return
	end

	canDamage = false
end

local function slash()
	if isSlashing then  -- اگر در حال حاضر ضربه زده شده باشد، هیچ اقدامی نکن
		return
	end

	isSlashing = true  -- زمانی که ضربه زده می‌شود، به حالت در حال ضربه می‌رود

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

	canDamage = true

	wait(0.5)  -- مدت زمان تاخیر برای جلوگیری از کلیک‌های اتوماتیک
	isSlashing = false  -- بعد از تاخیر، به حالت آماده برای ضربه جدید باز می‌گردد
end

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

Can you teach me what you did?

Basically you define enemies inside hostile table

Etc Team = {}–Enemies here

local Hostile:{[Team]:{[Team]:true}} = {
	[Teams.Police] = {
		[Teams.Prisoners] = true
	}

}

Makes police hostile to prisoners

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

local Hostile:{[Team]:{[Team]:true}} = {
	[Teams.Red] = {
		[Teams.Yellow] = true
	}

}

local function IsAlly(team:Team,otherTeam:Team):boolean
	return Hostile[team][otherTeam] or false
end

local tool = script.Parent
local Plr_ToolHolder--- define player who is holding the tool

local canDamage = false
local isSlashing = false  -- این متغیر جدید برای جلوگیری از کلیک‌های اتوماتیک اضافه می‌شود

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")::Humanoid?
	if not humanoid then return end
	local char = humanoid.Parent::Model
	local plr = Players:GetPlayerFromCharacter(char)::Player?

	if not plr or not IsAlly(Plr_ToolHolder,plr.Team) then 
		humanoid:TakeDamage(5)
	else
		return
	end

	canDamage = false
end

local function slash()
	if isSlashing then  -- اگر در حال حاضر ضربه زده شده باشد، هیچ اقدامی نکن
		return
	end

	isSlashing = true  -- زمانی که ضربه زده می‌شود، به حالت در حال ضربه می‌رود

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

	canDamage = true

	wait(0.5)  -- مدت زمان تاخیر برای جلوگیری از کلیک‌های اتوماتیک
	isSlashing = false  -- بعد از تاخیر، به حالت آماده برای ضربه جدید باز می‌گردد
end

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

i have blue error here
if not plr or not IsAlly(Plr_ToolHolder,plr.Team) then

It depends
If you have only 2 teams ingame then yeah hardcoding it is much better.

local Players = game:GetService("Players")


local tool = script.Parent
local Plr_ToolHolder:Player?=nil
tool:GetPropertyChangedSignal("Parent"):Connect(function():()
	Plr_ToolHolder = Players:GetPlayerFromCharacter(tool.Parent)
end)
Plr_ToolHolder = Players:GetPlayerFromCharacter(tool.Parent)

local canDamage = false
local isSlashing = false  -- این متغیر جدید برای جلوگیری از کلیک‌های اتوماتیک اضافه می‌شود

local function onTouch(otherPart)
	if not Plr_ToolHolder then return end--no tool holder
	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")::Humanoid?
	if not humanoid then return end
	local char = humanoid.Parent::Model
	local plr = Players:GetPlayerFromCharacter(char)::Player?

	if not plr or Plr_ToolHolder.Team~=plr.Team then 
		humanoid:TakeDamage(5)
	else
		return
	end

	canDamage = false
end

local function slash()
	if isSlashing then  -- اگر در حال حاضر ضربه زده شده باشد، هیچ اقدامی نکن
		return
	end

	isSlashing = true  -- زمانی که ضربه زده می‌شود، به حالت در حال ضربه می‌رود

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

	canDamage = true

	wait(0.5)  -- مدت زمان تاخیر برای جلوگیری از کلیک‌های اتوماتیک
	isSlashing = false  -- بعد از تاخیر، به حالت آماده برای ضربه جدید باز می‌گردد
end

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

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