Sword script doesn't play the animations

Hey.
I’m having a lot of troubles with this script. I’ve tried debugging it but I think I might need some help here.
The problem is that it plays only one animation from the table, but all the others are not playing. It also seems like when u equip the sword, the equip animation plays forever.

local script:

local tool = script.Parent
local remote = script:WaitForChild("Katana")

local enabled = false

tool.Equipped:Connect(function()
	remote:FireServer("Equipped")
	
end)

tool.Unequipped:Connect(function()
	remote:FireServer("Unequipped")
end)

tool.Activated:Connect(function()
	if enabled == false then
		enabled = true
		remote:FireServer("Attack")
		wait(0.7)
		enabled = false
		
	end
end)

script inside the local script:

local remote = script.Parent:WaitForChild("Katana")

remote.OnServerEvent:Connect(function(Player, Value)
	local character = workspace:WaitForChild(Player.Name)
	if Value == "Equipped" then
		print("equipped")
		
		local lookForAnimation = character:FindFirstChild("EquippedAnimation")
		if lookForAnimation then
			lookForAnimation:Destroy()
		end
		
		local animation = Instance.new("Animation", character)
		animation.Name = "EquippedKatana"
		animation.AnimationId = "rbxassetid://7425748912"
		local loader = character:WaitForChild("Humanoid"):LoadAnimation(animation)
		loader:Play()
		
	elseif Value == "Unequipped" then
		print("unequipped")
		local lookForAnimation = character:FindFirstChild("EquippedAnimation")
		if lookForAnimation then
			local loader = character:WaitForChild("Humanoid"):LoadAnimation(lookForAnimation)
			loader:Stop()
			lookForAnimation:Destroy()
		end
	elseif Value == "Attack" then
		print("Attack")
		local availableAttackAnimations = {121254126, 121254126, 121254126}
		local animation = Instance.new("Animation", character)
		animation.Name = "AttackAnimation"
		animation.AnimationId = "rbxassetid://"..tostring(availableAttackAnimations[math.random(3, #availableAttackAnimations)])
		local loader = character:WaitForChild("Humanoid"):LoadAnimation(animation)
		loader:Play()
		
		local alreadyHitObjects = {}
		
		script.Parent.Parent.Handle.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= Player.Name then
				for _, alreadyHit in pairs(alreadyHitObjects)do
					if alreadyHit == hit.Parent.Name then
						return		
					end
				end
				alreadyHitObjects[#alreadyHitObjects + 1] = hit.Parent.Name
				hit.Parent.Humanoid:TakeDamage(10)
			end
		end)
		
		
		
	
	end
end)

Thanks in advance!

issue is here you cant math.random (3,3) you have to mach.random(1,3)

animation.AnimationId = "rbxassetid://"..tostring(availableAttackAnimations[math.random(1, #availableAttackAnimations)])
1 Like

Thank you that worked but do you know why after I equip the katana and unequip it the animation is still playing?

1 Like

it may have loop on you have to remove loop

1 Like

wait 1 min ill show you what imean

1 Like


befor you save atack animation make sure the loop is off

1 Like

Wait i’ll try real quick one second

1 Like

Oh, I just realized, I totally forgot. this is an equip animation. In the script I made sure that it stops if the player unequips the sword, unlooping it just destroys it and when u equip it, u hold the sword for a sec and then u dont hold it anymore.

What i’m saying is that I made sure the animation stops if it gets unequipped, but it doesn’t seem to stop, it still plays.

1 Like

use playingTracks:Stop(0)

1 Like

Tried that before, Doesn’t work.

1 Like
remote.OnServerEvent:Connect(function(Player, Value)
	local character = workspace:WaitForChild(Player.Name)
	if character:FindFirstChild("EquippedKatana") then character.EquippedKatana:Destroy() end
	local animation = Instance.new("Animation", character)
	animation.Name = "EquippedKatana"
	animation.AnimationId = "rbxassetid://7425748912"
	local loader = character:WaitForChild("Humanoid"):LoadAnimation(animation)
	if Value == "Equipped" then
		loader:Play() end
	if Value == "Unequipped" then
			loader:Stop()
	end
	if Value == "Attack" then
		print("Attack")
		local availableAttackAnimations = {121254126, 121254126, 121254126}
		local animation = Instance.new("Animation", character)
		animation.Name = "AttackAnimation"
		animation.AnimationId = "rbxassetid://"..tostring(availableAttackAnimations[math.random(3, #availableAttackAnimations)])
		local loader = character:WaitForChild("Humanoid"):LoadAnimation(animation)
		loader:Play()

		local alreadyHitObjects = {}

		script.Parent.Parent.Handle.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= Player.Name then
				for _, alreadyHit in pairs(alreadyHitObjects)do
					if alreadyHit == hit.Parent.Name then
						return		
					end
				end
				alreadyHitObjects[#alreadyHitObjects + 1] = hit.Parent.Name
				hit.Parent.Humanoid:TakeDamage(10)
			end
		end)
	end
	end)
1 Like

make sure the loop is on now in Equipped animation

1 Like

It’s looped and still doesn’t work. This is quite strange, never happend to me something like this