The debounce on line 8 stops me from firing the attack more the once and allows me to fire it again in 5 on line 116. The debounce fails sometimes and allows me to fire the attack by repeatedly pressing Z. I have no idea why this happens please help.
Sorry about the script being so long, I felt that it would be best for you to see everything to be able to help me properly.
repeat wait() until game.Players.LocalPlayer.Character
local Player = game.Players.LocalPlayer
local weapon = Player.Character.weapon
local Mouse = Player:GetMouse()
local enabled = true
Mouse.KeyDown:connect(function(key)
if not enabled then
return
end
if key == "x" then
enabled = false
local AttackPart1 = Instance.new("Animation")
AttackPart1.AnimationId = "http://www.roblox.com/Asset?ID=382733312"
local animTrack1 = Player.Character.Humanoid:LoadAnimation(AttackPart1)
animTrack1:Play()
local Swing = game.ReplicatedStorage.SOUND.Swing:Clone()
Swing.Parent = Player.Character.Torso
wait()
Swing:Play()
local AttackPart2 = Instance.new("Animation")
AttackPart2.AnimationId = "http://www.roblox.com/Asset?ID=385127283"
local animTrack2 = Player.Character.Humanoid:LoadAnimation(AttackPart2)
animTrack2:Play()
local Swing2 = game.ReplicatedStorage.SOUND.Swing:Clone()
Swing2.Parent = Player.Character.Torso
Swing2.Pitch = 0.7
wait(0.25)
Swing2:Play()
debounce = false
for _, weaponSeg in pairs (weapon:GetChildren()) do
if weaponSeg.ClassName == "Part" then
weaponSeg.Touched:connect(function(part)
local Human = part.Parent:FindFirstChild("Humanoid")
local Blocked = part.Parent:FindFirstChild("Blocking")
if Human and not debounce and part.Parent ~= Player.Character and part.Parent.Name ~= "sword" and Blocked == nil then
debounce = true
Human:TakeDamage(math.random(20, 25))
local Slice = game.ReplicatedStorage.SOUND.Slice:Clone()
Slice.Parent = Player.Character.Torso
wait()
Slice:Play()
local Blood = game.ReplicatedStorage.PARTICLES.Blood:Clone()
Blood.Parent = part
Blood.Enabled = true
wait(0.4)
Slice:Destroy()
Blood.Enabled = false
wait(0.15)
Blood:Destroy()
elseif Human and not debounce and part.Parent ~= Player.Character and Blocked then
debounce = true
Human:TakeDamage(math.random(2, 5))
local Clash = game.ReplicatedStorage.SOUND.Clash:Clone()
Clash.Parent = Player.Character.Torso
wait()
Clash:Play()
local Sparks1 = game.ReplicatedStorage.PARTICLES.Sparks1:Clone()
Sparks1.Parent = part
Sparks1.Enabled = true
local Sparks2 = game.ReplicatedStorage.PARTICLES.Sparks2:Clone()
Sparks2.Parent = part
Sparks2.Enabled = true
wait(0.6)
Clash:Destroy()
Sparks1.Enabled = false
Sparks2.Enabled = false
wait(0.15)
Sparks1:Destroy()
Sparks2:Destroy()
end
end)
end
Swing:Destroy()
end
wait(2)
debounce = true
end
wait(5)
enabled = true
end)

4 comments
It likely has to do with where you placed your debounce, which is why correct tabbing is SO important.
Any idea where it went wrong then?
No because you didn't tab your code correctly and I don't wanna read through everything to figure it out
So how is it tabbed incorrectly?