How can I add multiple sword swinging animations?

I’ve tried multiple other posts on the forum but none of them worked since my script is slightly different then most, right now the sword only has one animation but I’m trying to add a random chance to play multiple different animations and I have no idea how to go about it or write it also for some reason putting the animation in a folder causes the swing animation to not play it could just be something I’m doing wrong but I’m not sure.
Here is the script and animations thanks

local tool = script.Parent
local player = nil
local players = game:GetService("Players")
local char = nil
local mouse = nil
local connection = nil
local IdleAnim = tool:WaitForChild("Idle")
local idleTrack = nil
local AttackTrack = nil
local AttackAnim = tool:WaitForChild("Attack")

local function onIdle(char, equipped)
	if not char then
		return
	end
	local hum = char:WaitForChild("Humanoid")
	local animator = hum:WaitForChild("Animator")
	if equipped then
		idleTrack = animator:LoadAnimation(IdleAnim)
		idleTrack:Play()
local	speed = hum.WalkSpeed
	elseif idleTrack then
		idleTrack:Stop()
		idleTrack:Destroy()
		idleTrack = nil
	end
end


local function onActivated()
	if not char then
		return
	end
	local hum = char:WaitForChild("Humanoid")
	local animator = hum:WaitForChild("Animator")
	
	if AttackTrack then
		return
    end			
			
		AttackTrack = animator:LoadAnimation(AttackAnim)
		AttackTrack:Play()
		task.wait(1.5)
		AttackTrack:Stop()
		AttackTrack:Destroy()
		AttackTrack = nil
	end



local function onEquipped()
	char = tool.Parent
	player = players:GetPlayerFromCharacter(char)
	if not player then
		return
	end
	mouse = player:GetMouse()
	connection = tool.Activated:Connect(onActivated)
	onIdle(char, true)
end

local function onUnequipped()
	player = nil
	mouse = nil
	connection:Disconnect()
	onIdle(char, false)
	char = nil
end

tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(onUnequipped)

Hi. Your script is actually pretty close. You can definitely have multiple attack animations and randomly choose one when the tool is activated.

The easiest way is to put all your Animation objects inside a folder, for example:

Tool
├─ Idle
├─ AttackAnimations
│  ├─ Attack1
│  ├─ Attack2
│  └─ Attack3
└─ LocalScript

Then instead of:

local AttackAnim = tool:WaitForChild("Attack")

you can do:

local AttackFolder = tool:WaitForChild("AttackAnimations")
local AttackAnimations = AttackFolder:GetChildren()

And inside onActivated():

local AttackAnim = AttackAnimations[math.random(1, #AttackAnimations)]
AttackTrack = animator:LoadAnimation(AttackAnim)
AttackTrack:Play()

So your onActivated() would look like:

local function onActivated()
	if not char then
		return
	end

	local hum = char:WaitForChild("Humanoid")
	local animator = hum:WaitForChild("Animator")

	if AttackTrack then
		return
	end

	local AttackAnim = AttackAnimations[math.random(1, #AttackAnimations)]

	AttackTrack = animator:LoadAnimation(AttackAnim)
	AttackTrack:Play()

	task.wait(1.5)

	if AttackTrack then
		AttackTrack:Stop()
		AttackTrack:Destroy()
		AttackTrack = nil
	end
end

One important thing: GetChildren() only gets the direct children of the folder, so make sure the animations are directly inside AttackAnimations, not inside another subfolder.

Also make sure every object in the folder is actually an Animation and has a valid AnimationId. If putting them in the folder makes the animation stop working, I’d first check that the folder contains the Animation instances themselves and that you’re not accidentally trying to load the Folder.

You could also filter the folder just to be safe:

local AttackAnimations = {}

for _, animation in ipairs(AttackFolder:GetChildren()) do
	if animation:IsA("Animation") then
		table.insert(AttackAnimations, animation)
	end
end

Then you can have as many attack animations as you want and Roblox will randomly select one each time the sword is swung.

Also, I’d recommend using AttackTrack.Stopped:Wait() instead of always waiting exactly 1.5 seconds if all your animations have different lengths. That way the script automatically waits for the animation to finish

2 Likes

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