Glitch with katana script

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!

In this section here

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

In the first if statement it makes checks if the cycle == 0 and inside you set the cycle to 1.

In the second if statement it checks if the cycle == 1 which you set to one in the first if statement which causes it to play both

i would do

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

basically just make it one big if statement

This works perfectly, thanks so much :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.