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
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