Sword animation Idle\Walk gets broken after swinging

https://gyazo.com/6e7e5aa596e74e76a7a7689ae41005d7

What is the problem ??:
so basically when I walk or idle it is ok but when I swing everything changes and it break and it will only work again only when i equip and unequip
(I am using client cast so I putted it to server)

Local client to hand the animations :

local Tool = script.Parent
local Player = Tool.Parent.Parent

print(Player)
Player.CharacterAdded:Wait()    
local Character = Player.Character
local Humanoid,Animate =  Character:WaitForChild("Humanoid"),Character:WaitForChild("Animate")

local Equipped = false



Tool.Equipped:Connect(function()
	for _, playingTracks in pairs(Humanoid:GetPlayingAnimationTracks()) do
		playingTracks:Stop()
	end
	Equipped = true
	--Character.Animate.walk.WalkAnim.AnimationId = "rbxassetid://11420832921"
	--Character.Animate.toolnone.ToolNoneAnim.AnimationId = "rbxassetid://11420835044"
	--Character.Animate.idle.Animation1.AnimationId = "rbxassetid://11420835044"
	--Character.Animate.idle.Animation2.AnimationId = "rbxassetid://11420835044"
	local Animation = Instance.new("Animation")

	Animation.AnimationId = "rbxassetid://11420832921"

	local Walk = Humanoid:LoadAnimation(Animation)
	--Walk.Priority = Enum.AnimationPriority.Movement

	local Animation = Instance.new("Animation")

	Animation.AnimationId = "rbxassetid://11420835044"

	local Idle = Humanoid:LoadAnimation(Animation)	
	--Idle.Priority = Enum.AnimationPriority.Idle
	--local Connection2 

	--Connection2 = Humanoid.Running:Connect(function(Speed)
	--	print(Speed)
	--	if Speed > 0  then
	--		if not Walk.IsPlaying and Idle.IsPlaying then

	--			Idle:Stop()
	--			Walk:Play()
	--		end	
	--	else
	--		if  Walk.IsPlaying and not Idle.IsPlaying then
	--			Idle:Play()
	--			Walk:Stop()
	--		end			
	--	end
	--end)
	local Connection2 

	Connection2 = game:GetService("RunService").Heartbeat:Connect(function()
		local Magnitude = Humanoid.MoveDirection.Magnitude
		if Humanoid.MoveDirection == Vector3.new(0,0,0)  then
			if  Walk.IsPlaying and not Idle.IsPlaying  then
			
				Idle:Play()
				Walk:Stop()
			end
		else
			if not Walk.IsPlaying and Idle.IsPlaying then
				
				Walk:Play()
				Idle:Stop()
			end
		end
	end)


	Idle:Play()

	--Humanoid.StateChanged:Connect(function(State)
	--	if State == Enum.HumanoidStateType.Landed  then
	--		--Idle:Play()

	--		if Magnitude == 0  then
	--			Idle:Play() 
	--			I
	--		end
	--		--	Walk:Play()

	--		--end		
	--	end
	--end)
	script.Parent.IdlePlay.OnClientEvent:Connect(function()
		wait(2)
		Idle:Play()
		
	end)
	Tool.Unequipped:Connect(function()
		Equipped = false
		for i,v in pairs(Player.Character.Humanoid:GetPlayingAnimationTracks()) do
			v:Stop()
		end
		
	
		Connection2:Disconnect()
		for i,v in pairs(Player.Character.Humanoid:GetPlayingAnimationTracks()) do
			v:Stop()
		end
		Walk:Stop()
		Idle:Stop()
		local Magnitude = Humanoid.MoveDirection.Magnitude

		if Magnitude == 0  then
			local WalkID = Animate.idle.Animation1.AnimationId

			local Animation = Instance.new("Animation")

			Animation.AnimationId = WalkID

			local Idle2 = Humanoid:LoadAnimation(Animation)

			Idle2:Play()
		else
			local WalkID = Animate.walk.WalkAnim.AnimationId

			local Animation = Instance.new("Animation")

			Animation.AnimationId = WalkID

			local Walk2 = Humanoid:LoadAnimation(Animation)

			Walk2:Play()


			local Connection
			Connection = Humanoid.Running:Connect(function(Speed)
				if Speed == 0 then
					Walk2:Stop()

					Connection:Disconnect()
				end
			end)

		end



	end)

end)


	local Connection2 

	Connection2 = game:GetService("RunService").Heartbeat:Connect(function()
		local Magnitude = Humanoid.MoveDirection.Magnitude
		if Humanoid.MoveDirection == Vector3.new(0,0,0)  then
			if  Walk.IsPlaying and not Idle.IsPlaying  then
			
				Idle:Play()
				Walk:Stop()
			end
		else
			if not Walk.IsPlaying and Idle.IsPlaying then
				
				Walk:Play()
				Idle:Stop()
			end
		end
	end)


	Idle:Play()

There is other script to hand the swing which is in server:

local DataModule = require(game.ServerScriptService.DataCore.DataManager)
local ClientCast = require(game.ServerStorage.ClientCast)

--[[Objects]]
local Tool = script.Parent
local Player = script.Parent.Parent.Parent
local PlayerDataRemote = game:GetService("ReplicatedStorage").Remote.PlayerData
local SwordName = "CurvedSword"
local PlayerData = DataModule.ReturnData(Player,SwordName)
local Damage = PlayerData.Damage
--[[Remote]]
local AnimationPlayRemote = game.ReplicatedStorage.Remote.AnimationPlaying
--We will now start using our client caster
local Character = Player.Character


local Humanoid = Character:WaitForChild("Humanoid")



local casterParams = RaycastParams.new()
casterParams.FilterDescendantsInstances = {Player.Character or Player.Parent.Character,Tool.Blade}
casterParams.FilterType = Enum.RaycastFilterType.Blacklist
local Caster = ClientCast.new(Tool.Blade,casterParams)
local Debounce = {}
Caster:Stop()	
Caster:SetOwner(nil)
Caster.Collided:Connect(function(RaycastResult)
	local HitHumanoid = RaycastResult.Instance.Parent:FindFirstChild("Humanoid")
	if HitHumanoid == nil then return end
	if Debounce[HitHumanoid] then
		return
	end
	Debounce[HitHumanoid] = true

	HitHumanoid.Health = HitHumanoid.Health - Damage
	coroutine.wrap(function()
		wait(0.75) -- 75
		Debounce[HitHumanoid] = false
	end)()

	

end)

Tool.Activated:Connect(function()
	if (os.clock() -  PlayerData.Debounce ) < PlayerData.Cooldown then return end

	PlayerData.Debounce = os.clock()
	local Sounds = {Tool.Blade["Sword Swing"],Tool.Blade["Sword Swing Metal Heavy"]}
	local RandSound = math.random(1,#PlayerData.Animations)

	Sounds[RandSound]:Play()
	local Rand = math.random(1,#PlayerData.Animations)
	AnimationPlayRemote:FireAllClients(PlayerData.Animations[Rand],Player)
	local animator = Instance.new("Animator")
	animator.Parent = Player.Character.Humanoid
	-- Create a new "Animation" instance and assign an animation asset ID
	local Animation = Instance.new("Animation")

	Animation.AnimationId = PlayerData.Animations[Rand]
	local SwordTrack = Humanoid:LoadAnimation(Animation)
	SwordTrack:Play(0)
	Caster:Start()
	coroutine.wrap(function()

		PlayerDataRemote:FireClient(Player, DataModule.ReturnData(Player,SwordName) )
		Humanoid.WalkSpeed = 11
		wait(0.75)
		Caster:Stop()	
		Humanoid.WalkSpeed = 16

		end) ()
	SwordTrack.Stopped:Wait()

	
	
	script.Parent.IdlePlay:FireClient(Player)


	
	
	
end)





Tool.Unequipped:Connect(function()
	
	Caster:Stop()

end)


There’s the local script for animations so it shows it to the client good(This only been used in the server script where swing happens)

--Animation manager


--[[Services]]
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
--[[Import]]

--[[Dirs]]
local Remote = ReplicatedStorage.Remote

--[[Variables]]

--[[Objects]]

--a
--[[Remote]]

local AnimationRemote = Remote.AnimationPlaying





--[[Functions]]

--[[Application]]

AnimationRemote.OnClientEvent:Connect(function(AnimationID,Player)
	local Character = Player.Character
	local Humanoid = Character:WaitForChild("Humanoid")
	local animator = Instance.new("Animator")
	animator.Parent = Player.Character.Humanoid
	-- Create a new "Animation" instance and assign an animation asset ID
	local Animation = Instance.new("Animation")
	
	Animation.AnimationId = AnimationID
	local SwordTrack = Humanoid:LoadAnimation(Animation)
	SwordTrack.Priority = Enum.AnimationPriority.Action
	SwordTrack:Play(0)

end)

So what I want to get is that when he swings it does not make it animation less for the walk and idle

1 Like

The video is a bit confusing, what is breaking during the animation?

1 Like

you can see after he swing there’s no walk or idle animation
sorry for the confusing

Okay, whats the Idle:Play() supposed to do at the bottom? Try putting a few prints around the spots that you think is causing this issue and see what happens.

So when he start holding it in the begin he will do the idle animation(I may delete the question and upload it again with more details do you think that is ok ? )

Don’t reupload, just edit your post.

Ok I will do it now :slight_smile: after like 10 minutes it will be done

Okay, just reply to me when you’re done! :slight_smile:

changed the video too :slight_smile: if you want any more information I am here

Okay, why is SwordTrack playing for 0 seconds?

I heard I should do it 0 so sometimes it does not start from the middle(I didn’t have this problem but did this) you can say no reason

Well what happens if you change the value and what is SwordTrack for?

Nothing happens and the sword track is for animation track

Not sure about this but it seems like SwordTrack plays when the Idle plays due to SwordTrack playing when a client event happens.

1 Like

I will need now to go study when i am back I will let you know and will see if your theory is right

Okay, I’m not gonna solve it from here anyways, cya.

i added this, for some reason magnitude stuff is kinda weird sometimes, to solve this i used a new system with Movement keys

local UIS = game:GetService("UserInputService")

UIS.InputEnded:Connect(function(key, chat) -- This is '' Input Ended'' it will be activated after

'' pressing W ---- but after you release W, this will be activated"

	if chat then return end

	if key.KeyCode == Enum.KeyCode.W then
	pressedW = false -- make sure to disable it -- will not play any animations
         
        if wPressed == false and sPressed == false and aPressed == false and dPressed == false then	
          --Play Idle Animation
          -- Stop walking animation	
       end		
	end
end

by the way, this only works for the W key, BUT, i dind’t added the complex part

this works really nice, dind’t saw any bugs until now ( 1 year using this )

you will need to add the InputBegan too, after pressing W, A, S , D and activate some boolvalue to check which key has been pressed

since it will be really messed up after you add Input ENDED W, A , S and D

because after releasing any key, (WASD), it will play the idle animation

for example

Press W first, then A

but you released the W key, but still walking to the Left side (A) key

this will cause the issue i told you, odd animations playing and stopping

for this i added something like:

local pressedW = false
local pressedA = false
local pressedD = false
local pressedS = false

i don’t know if there is another way around it, but this never caused my any lag spike or smh

check on each ‘’ if statement if all boolvalues are [False] … if not, then don’t activate the idle animation, that means the user is walking,

i added this since the magnitude stuff dind’t worked too smooth, i had some problems trying to check when the player stopped moving and when IDLE