How would I make a function have an "or"

Hey everyone,

I am working on a sword combat mechanic, and the enemy that gets hit with the Handle of the sword gets damaged. This is annoying for swords that have long blades, because the handle of the sword is the part that damages others, not the blade. How can I make both the blade and the handle damage an enemy?

function attacks.Sword(player, tool)
	local connection = tool.Handle.Touched:Connect(function(part)  -- This is where the title of the post comes in, I tried making a tool.Handle OR tool.Handle.blade
		local enemyHumanoid = FindEnemyHumanoid(part)
		if enemyHumanoid then
			require(enemyFunctions).DamageEnemy(player, tool, enemyHumanoid)
		end
	end)
	local cooldown = Instance.new("Model")
	cooldown.Name = player.Name
	cooldown.Parent = script
	game:GetService("Debris"):AddItem(cooldown, replicatedStorage.Items[tool.Name].Cooldown.Value)
	local tag = Instance.new("Model")
	tag.Name = "Damage"
	tag.Parent = tool
	game:GetService("Debris"):AddItem(tag, 0.5)
	wait(0.6)
	connection:Disconnect()
end

Thanks,
Mustang

You would need to individually assign a touched event to both.

New code:

function attacks.Sword(player, tool)
	local function OnTouched(part)
		local enemyHumanoid = FindEnemyHumanoid(part)
		
		if enemyHumanoid then
			require(enemyFunctions).DamageEnemy(player, tool, enemyHumanoid)
		end
	end
	
	local Connections = {}
	
	Connections.Handle = tool.Handle.Touched:Connect(function(part)  -- This is where the title of the post comes in, I tried making a tool.Handle OR tool.Handle.blade
		OnTouched(part)
	end)
	
	Connections.Blade = tool.Handle.blade:Connect(function(part)
		OnTouched(part)
	end)
	
	local cooldown = Instance.new("Model")
	cooldown.Name = player.Name
	cooldown.Parent = script
	
	task.delay(replicatedStorage.Items[tool.Name].Cooldown.Value, function()
		cooldown:Destroy()
	end)
		
	local tag = Instance.new("Model")
	tag.Name = "Damage"
	tag.Parent = tool
	
	task.delay(0.5, function()
		if tag:IsDescendantOf(workspace) then
			tag:Destroy()
		end
	end)
	
	task.wait(0.6)
	
	for i, connection in pairs(Connections) do
		connection:Disconnect()
	end
end
1 Like

yes, and the touched events would have to use the same cooldown for it to not do double the damage.

1 Like

Thank you! This works very well along with what @DestDeHat said.

No problem. If you have any more questions, feel free to ask.

1 Like