-
What do you want to achieve?
I wish to create a sword that can hit an enemy multiple times without having to untouch the enemy to damage it again. -
What is the issue?
To deal damage, you have to hit the enemy, and untouch it with the sword just to hit it again. It’s a system that doesn’t flow well and I just want the player to be able to hit the enemy while standing right next to it. When I tested, if I kept the sword touching the enemy while attacking it, only the first attack would deal damage, I believe this is due to the Touch function only running on the first touch and believes that the sword is still touching the enemy. -
What solutions have you tried so far?
I was and still am assuming it has something to do with the Touched function. I tried tagging enemies and using raycasts but I was unable to do so successfully. I looked for solutions on here but was unable to find any that applied to my situation.
If anyone has any ideas I’m up for grabs.
My Sword Script:
--==-- Object Variables --==--
local Sword = script.Parent
local Handle = Sword:WaitForChild("Handle")
local SwordAttackSound = Handle:WaitForChild("SwordSlash")
local SwordUnsheathSound = Handle:WaitForChild("Unsheath")
local SlashAnim = Sword:WaitForChild("SlashAnim")
--==-- Configurations --==--
local Configuration = require(Sword:WaitForChild("ConfigurationModule"))
local DamageRange = Configuration.DamageRange
local Cooldown = Configuration.Cooldown
local SwordName = Configuration.SwordName
Sword.Name = SwordName
local SwordColor = Configuration.SwordColor
Handle.Color = SwordColor
local SellValue = Configuration.SellValue
local Purchasable = Configuration.Purchasable
if Purchasable then
local Shop = Configuration.Shop
local SwordPrice = Configuration.SwordPrice
local RequiredLevel = Configuration.RequiredLevel
end
if Configuration.SwordSlashID ~= nil then
SwordAttackSound.SoundId = Configuration.SwordSlashID
end
if Configuration.SwordUnsheathID ~= nil then
SwordUnsheathSound.SoundId = Configuration.SwordUnsheathID
end
Sword.ToolTip = DamageRange[1].."-"..DamageRange[2].." DMG"
if Configuration.SwingAnim ~= nil then
SlashAnim.AnimationId = "rbxassetid://"..Configuration.SwingAnim
end
if Configuration.EquipAnim ~= nil then
EquipAnim.AnimationId = "rbxassetid://"..Configuration.EquipAnim
end
if Configuration.IdleAnim ~= nil then
IdleAnim.AnimationId = "rbxassetid://"..Configuration.IdleAnim
end
--==-- Misc Variables --==--
local CanSwing = true
local CanDamage = false
--==-- Functions --==--
function Equipped()
SwordUnsheathSound:Play()
end
function UnEquipped()
--> Ignore this and the Equipped function
end
function Slash()
if CanSwing then
CanDamage = true
CanSwing = false
local Humanoid = script.Parent.Parent:WaitForChild("Humanoid")
local slashAnimTrack = Humanoid:LoadAnimation(SlashAnim)
SwordAttackSound:Play()
slashAnimTrack:Play(0)
task.wait(Cooldown) --> Cooldown period
CanDamage = false
CanSwing = true
end
end
function SwordHit(hit)
if hit:FindFirstChild("Humanoid") and hit.Parent ~= Sword.Parent then
if CanDamage and not CanSwing then
CanDamage = false
local EnemyHumanoid = hit:FindFirstChild("Humanoid")
EnemyHumanoid:TakeDamage(math.random(DamageRange[1], DamageRange[2]))
end
end
end
--==-- Function Callings --==--
Sword.Equipped:Connect(Equipped)
Sword.Unequipped:Connect(UnEquipped)
Sword.Activated:Connect(Slash)
Handle.Touched:Connect(SwordHit)
The CanDamage and CanSwing are debugs to force a cooldown onto the sword. I’m only accepting solutions that keep the cooldown the same.