Alternating Animations and Audios in a Sword

Developers of ROBLOX,
I was wondering how I could alternate animations and audios in a sword. Currently, my script runs fine, however I would prefer if they could go back and forth instead of picking a random animation and playing it. How could I go about doing this? (Sorry if this is an obvious solution, I am not much of a scripter, and there was nothing on YT.) Here is my current code, for reference:

--//Attacking Animations\\--
local UserInputService = game:GetService("UserInputService")
local Tool = script.Parent
local Player = game.Players.LocalPlayer
local CanAttack = true
local AttackAudioA = script.Parent.Handle.Swing1
local AttackAudioB = script.Parent.Handle.Swing2

UserInputService.InputBegan:Connect(function(InputObject)
	if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		local AttackA = script.Parent.Animations.Attack1
		local AttackB = script.Parent.Animations.Attack2
		local Track1 = script.Parent.Parent.Humanoid:LoadAnimation(AttackA)
		local Track2 = script.Parent.Parent.Humanoid:LoadAnimation(AttackB)
		local anim = math.random(1,2)
		if anim == 1 and CanAttack == true then
			CanAttack = false
			Track1:Play()
			AttackAudioA:Play()
			wait(0.45)
			CanAttack = true
			else if anim == 2 and CanAttack == true then
			CanAttack = false
			Track2:Play()
			AttackAudioB:Play()
			wait(0.6)
			CanAttack = true
			end
		end
	end
end)

Feel free to comment on my current code as well, if you think anything could be improved.
Thanks, toe_s :slightly_smiling_face:

1 Like

You can make a variable of 1 called something like AttackNumber and, increase it by 1 every attack. So instead of using math.random use AttackNumber to play the specific animation.
Now all you need to do is set AttackNumber back to 1 when it reaches the limit.
Example:

local Anim_Name = "Attack_"
local Attack_Animations = Player.Backpack.Animations -- where your animations are stored
local Attack_Num = 1

InputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local Animation = Player.Character.Humanoid:LoadAnimation(Attack_Animations[Anim_Name..Attack_Num])
		Animation:Play()
		if Attack_Num < #Attack_Animations:GetChildren() then
			Attack_Num = Attack_Num + 1
		else
			Attack_Num = 1
		end
	end
end)

this works with any number of animations.

3 Likes

I tried your code, however the number never returned to one. I messed around with it a little bit and now it works for two animations. (and I realized I had more than 2 items in my animation folder :man_facepalming: )
Code that worked for me was this:
UserInputService.InputBegan:Connect(function(InputObject)
if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
local Animation = script.Parent.Parent.Humanoid:LoadAnimation(Attack_Animations[Anim_Name…Attack_Num])
Animation:Play()
if Attack_Num == 1 then
Attack_Num = Attack_Num + 1
else if Attack_Num == 2 then
Attack_Num = 1
end
end
end
end)

Thanks for the help and good luck with your game! :slightly_smiling_face: