Attack_Clone.Touched:Connect(function(hit) -- Attack clone is my attack
local DamageCooldown = 0.5
local DamageDebounce = false
local Damage = DefaultDamage + player.Attributes.Attack.Value -- This caculates the damage with how much Attack the player has
local xLifetime = 5
if hit.Parent.Name == player.Name then return end -- Makes it so you can't damage yourself
if not hit.Parent:FindFirstChild("HitFolder") then
local Folder = Instance.new("Folder", hit.Parent)
Folder.Name = "HitFolder"
end
if not hit.Parent:FindFirstChild("HitFolder"):FindFirstChild(player.Name) then
local x = Instance.new("IntValue", hit.Parent:FindFirstChild("HitFolder"))
x.Name = player.Name
game.Debris:AddItem(x, xLifetime)
hit.Parent.Humanoid:TakeDamage(Damage)
wait(DamageCooldown)
DamageDebounce = false
end
end)
The issue is that you are checking something which is unnecessary;
if not hit.Parent:FindFirstChild("HitFolder"):FindFirstChild(player.Name) then
I am still quite confused how you came up with this and how this would work.
Note that using :FindFirstChild() on something which possibly could be nil (aka on another :FindFirstChild() would break the script.
Instead it should be:
local hum = hit.Parent:FindFirstCHildWhichIsA("Humanoid")
if hum then
Note that the DamageCooldown, DamageDebounce, Damage and Lifetime should be kept outside of the .Touched event.
As else the DamageDebounce would not work, due to a new function being called every time.
For the other variables it is pointless to repeatly store them in different functions even though they are one and the same.
And note you have not used an if statement to check for the DamageDebounce
Please mark my previous post as the solution if that answers your question.
Attack_Clone.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if hit and humanoid then
humanoid.Health -= 5
end
end)
local player = script.Parent.Parent.Parent --i made this up assuming this script is inside a tool
local character = player.Character --this is retrieved from the previous reference
local DamageCooldown = 0.5
local DamageDebounce = false
local DefaultDamage = 10 --i just made this up
local IntValueLifetime = 5
local Attack_Clone = script.Parent.Handle --i made this up to
Attack_Clone.Touched:Connect(function(hit) -- Attack clone is my attack
local Damage = DefaultDamage + player.Attributes.Attack.Value -- This caculates the damage with how much Attack the player has
if DamageDebounce then
return
end
if hit.Parent == character then
return
end
if hit.Parent:FindFirstChild("Humanoid") then
DamageDebounce = true
local enemyModel = hit.Parent
local enemyHumanoid = enemyModel:WaitForChild("Humanoid")
if not enemyModel:FindFirstChild("HitFolder") then
local Folder = Instance.new("Folder")
Folder.Parent = enemyModel
Folder.Name = "HitFolder"
if not Folder:FindFirstChild(player.Name) then
local IntValue = Instance.new("IntValue")
IntValue.Parent = Folder
IntValue.Name = player.Name
game.Debris:AddItem(IntValue, IntValueLifetime)
enemyHumanoid:TakeDamage(Damage)
else
local IntValue = Folder:FindFirstChild(player.Name)
game.Debris:AddItem(IntValue, IntValueLifetime)
enemyHumanoid:TakeDamage(Damage)
end
else
local Folder = enemyModel:FindFirstChild("HitFolder")
if not Folder:FindFirstChild(player.Name) then
local IntValue = Instance.new("IntValue")
IntValue.Parent = Folder
IntValue.Name = player.Name
game.Debris:AddItem(IntValue, IntValueLifetime)
enemyHumanoid:TakeDamage(Damage)
else
local IntValue = Folder:FindFirstChild(player.Name)
game.Debris:AddItem(IntValue, IntValueLifetime)
enemyHumanoid:TakeDamage(Damage)
end
end
task.wait(DamageCooldown)
DamageDebounce = false
end
end)
I made some variables up at the beginning of the script to try and fill in the gaps, you’ll likely need to replace them but the Touched event itself should be good to go.