How do make sword swing animations play different animation every time

Hey There Robloxians!
This is my first time using Devforum. So I was recently trying edit the Roblox classic sword to make a sword to make it better, everything worked completely fine but I wanted it to play different animations every time used so I made a table with animation IDs and connected it with a math.random like so:

SlashAnimations = {
"12864626309",
"12864568667",
"12791260905"
}
local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
		Name = "R15Slash",
		AnimationId = BaseUrl..SlashAnimations[math.random(1, #SlashAnimations)],
		Parent = Tool
	})

The problem is I am not getting the result as I wanted. The sword gets a different animation every time I rejoin but I want it to play different animation in a pattern (like first animation then second then third then first and so on)

What changes should I make in the script?

2 Likes

Instead of writing the entire AnimationId, just paste the differend Ids in an animation and then copy the big link that it gives you. Then put all of those links in the table. The rest of the script looks fine to me.

1 Like

You are declarating a global variable, what you have to do is create a local variable inside the Tooll.Activated event. Ej:

local Tool = script.Parent
local Anims = {"12864626309","12864568667","12791260905"}

Tool.Activated:Connect(function()
   AnimationId = BaseUrl..Anims[math.random(1, #Anims)]
end)
1 Like

You Should probably make an animation track for each of the animations instead of a new one each time but anyway to play the next animation in the the table each time you just need to get the animation that is right after the previous one:

local CurrentAnimation = 0

local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
		Name = "R15Slash",
		AnimationId = BaseUrl..SlashAnimations[CurrentAnimation % 3 + 1],
		Parent = Tool
	})
CurrentAnimation  += 1

The % is returns the remainder of division between two number we start at 0 and add 1 because when we get to 3, 3 % 3 would be 0 which would give us nil so we add 1 to get the first value again and we chose 3 because that’s how many moves you have

Hey! I tried that but it does the same thing like before :frowning:

Hey, thanks for helping, unfortunately It didn’t worked.

Hey, it didn’t gave me the result I wanted and it is still playing same animation again and again, any other solutions?

are you loading the slash anim after creating it?

yes, i am first publishing and even copying the right ID but still

I mean are you using animator:LoadAnimation() or Humanoid:LoadAnimation()? also sorry for late reply

I am using Humanoid:LoadAnimation()

can you show me larger part of your code?

Tool = script.Parent
Handle = Tool:WaitForChild("Handle")
Debounce = true
Trail = script.Parent.Handle.SwordSlashTrail
SwordLight = script.Parent.Handle.SwordLight
HitBox = script.Parent.Handle.HitBox
HitBoxS = script.Parent.Handle.HitBox.DamageScript
EffectS = script.Parent.Handle.HitBox.Effects

function Create(ty)
	return function(data)
		local obj = Instance.new(ty)
		for k, v in pairs(data) do
			if type(k) == 'number' then
				v.Parent = obj
			else
				obj[k] = v
			end
		end
		return obj
	end
end

local BaseUrl = "rbxassetid://"

Players = game:GetService("Players")
Debris = game:GetService("Debris")
RunService = game:GetService("RunService")

DamageValues = {
	SlashDamage = 0,
}


SlashAnimations = {
	"rbxassetid://12905465996",
}

Grips = {
	Up = CFrame.new(0, 0, -1.70000005, 0, 0, 1, 1, 0, 0, 0, 1, 0),
	Out = CFrame.new(0, 0, -1.70000005, 0, 1, 0, 1, -0, 0, 0, 0, -1)
}

Sounds = {
	Slash = Handle:WaitForChild("SwordSlash"),
	Unsheath = Handle:WaitForChild("Unsheath")
}

ToolEquipped = false


Tool.Grip = Grips.Up
Tool.Enabled = true



function IsTeamMate(Player1, Player2)
	return (Player1 and Player2 and not Player1.Neutral and not Player2.Neutral and Player1.TeamColor == Player2.TeamColor)
end

function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

function Blow(Hit)
	if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
		return
	end
	local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
	if not RightArm then
		return
	end
	local RightGrip = RightArm:FindFirstChild("RightGrip")
	if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
		return
	end
	local character = Hit.Parent
	if character == Character then
		return
	end
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid or humanoid.Health == 0 then
		return
	end
	local player = Players:GetPlayerFromCharacter(character)
	if player and (player == Player or IsTeamMate(Player, player)) then
		return
	end
	UntagHumanoid(humanoid)
	TagHumanoid(humanoid, Player)	
	humanoid:TakeDamage(Damage)
end


function Attack()
	Damage = DamageValues.SlashDamage
	Sounds.Slash:Play()

	if Humanoid then
		if Humanoid.RigType == Enum.HumanoidRigType.R6 then
			local Anim = Instance.new("StringValue")
			Anim.Name = "toolanim"
			Anim.Value = "Slash"
			Anim.Parent = Tool
		elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
			local Anim = Tool:FindFirstChild("R15Slash")
			if Anim then
				local Track = Humanoid:LoadAnimation(Anim)
				Trail.Enabled = true
				Track:Play(0)
				HitBoxS.Enabled = true
				EffectS.Enabled = true
				SwordLight.Brightness = 2
				task.wait(0.3)
				SwordLight.Brightness = 0
				task.wait(0.2)


				Trail.Enabled = false
			end
		end
	end	
end



Tool.Enabled = true
LastAttack = 0

function Activated()
	if not Tool.Enabled or not ToolEquipped or not CheckIfAlive() then
		return
	end
	Tool.Enabled = false
	local Tick = RunService.Stepped:wait()
	if (Tick - LastAttack < 1) then
		task.wait(0.5)
		Trail.Enabled = true
		Attack()
		HitBoxS.Enabled = true
		EffectS.Enabled = true
		SwordLight.Brightness = 2
		task.wait(0.3)
		SwordLight.Brightness = 0
		task.wait(0.2)
		
		HitBoxS.Enabled = false
		EffectS.Enabled = false
		
		Trail.Enabled = false
	else
		Trail.Enabled = true
		Attack()
		HitBoxS.Enabled = true
		EffectS.Enabled = true
		SwordLight.Brightness = 2
		task.wait(0.3)
		SwordLight.Brightness = 0
		task.wait(0.2)
		
		HitBoxS.Enabled = false
		EffectS.Enabled = false
		
		Trail.Enabled = false
	end
	
	
	LastAttack = Tick
	local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
		Name = "R15Slash",
		AnimationId = SlashAnimations[math.random(1, #SlashAnimations)],
		Parent = Tool
	})
	
	local LungeAnim = (Tool:FindFirstChild("R15Lunge") or Create("Animation"){
	})
	Tool.Enabled = true
	
end

function CheckIfAlive()
	return (((Player and Player.Parent and Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid.Health > 0 and Torso and Torso.Parent) and true) or false)
end

function Equipped()
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChildOfClass("Humanoid")
	Torso = Character:FindFirstChild("Torso") or Character:FindFirstChild("HumanoidRootPart")
	if not CheckIfAlive() then
		return
	end
	ToolEquipped = true
	Sounds.Unsheath:Play()
end

function Unequipped()
	Tool.Grip = Grips.Up
	ToolEquipped = false
end

Tool.Activated:Connect(Activated)
Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(Unequipped)

Connection = Handle.Touched:Connect(Blow)

you only have one animation that can be played?

It’s because you’re not deleting the old animation after you change it
also I reccomend doing something like this instead of creating a new animation each time:

SlashAnimations = {
	Humanoid:LoadAnimation(Create("Animation", {"rbxassetid://12905465996"})),
}

local R15Slash = SlashAnimations[1]

--when you need to change it
R15Slash = SlashAnimations[CurrentAnimation % 3 + 1] --like in my post above

--then just play R15Slash normally when you need it

adjust this to fit your script

local Animation = {
Humanoid:LoadAnimation(animationname1)
Humanoid:LoadAnimation(animationname2)
}

Tool.Activated:Connect(function()
Animation[math.random(#Animation)]:Play
end)

If it didn’t work, I will show other way since I don’t test yet.

Oh I removed other animations before

Still it is not working, it breaks whole script

nope, it is playing only 1 animation like before

It isnt that complicated… It’s just as simple as this:

local swing = 1

local anim1 = swing1 -- anim
local anim2 = swing2 -- anim
local anim3 = swing3 -- anim

if swing == 1 then
	swing = 2
	
	humanoid.Animator:LoadAnimation(anim1):Play()
	
elseif swing == 2 then
	swing = 3

	humanoid.Animator:LoadAnimation(anim1):Play()
elseif swing == 3 then
	swing = 1

	humanoid.Animator:LoadAnimation(anim1):Play()
end