Hello!
I’m trying to make a script that makes a katana swing. I made two animations for this and they are supposed to alternate each time the weapon is used. The problem is that the code plays both animations in succession despite only clicking once. Here’s an example:
Here’s the code:
local debounce = false
local attacking = false
local cycle = 0
function damage(part)
if attacking == true then
attacking = false
script.Parent.Handle.Hit.PitchShiftSoundEffect.Octave = Random.new():NextNumber(0.75, 1)
script.Parent.Handle.Hit:Play()
part.Parent.Humanoid.Health -= 6.25
part.Parent.Humanoid.Health -= 6.25
part.Parent.Humanoid.Health -= 6.25
part.Parent.Humanoid.Health -= 6.25
end
end
script.Parent.Weapon.Touched:Connect(damage)
function attack()
local random = math.random(1, 2)
local animation = Instance.new("Animation")
local animation1 = Instance.new("Animation")
if debounce == false then
if cycle == 0 then
debounce = true
cycle = 1
animation.AnimationId = "rbxassetid://112385709841563"
script.Parent.Parent.Humanoid.Animator:LoadAnimation(animation):Play()
wait(0.14)
script.Parent.Handle.Trail.Enabled = true
attacking = true
wait(0.2)
script.Parent.Handle.Trail.Enabled = false
attacking = false
wait(0.125)
debounce = false
end
if cycle == 1 then
debounce = true
cycle = 0
animation.AnimationId = "rbxassetid://82205929928253"
script.Parent.Parent.Humanoid.Animator:LoadAnimation(animation):Play()
wait(0.14)
script.Parent.Handle.Trail.Enabled = true
attacking = true
wait(0.2)
script.Parent.Handle.Trail.Enabled = false
attacking = false
wait(0.125)
debounce = false
end
end
end
script.Parent.Activated:Connect(attack)
Any help would be greatly appreciated. Thanks!