Sword Running and Idle playing after the Player unequips

As of now, I’m tackling a Metal Gear Rising-esque weapons system. Starting off with equipping/unequipping the blade. However, the equipped run/idle animation continues to play AFTER the weapon has been unequipped.

I have looked through posts regarding bugs around stopping Animations. I’ve tried ipairs, and stopping it using the same Humanoid function in the Unequip statement. Oh and by the way, the LocalScript goes in ‘StarterPack’

--//Service Variables\\--
local UserInputService = game:GetService("UserInputService")
local Debounce = false

--//Replicated Storage\\--
local Weapons_Folder = game.ReplicatedStorage:WaitForChild("Weapons")
local WPChar_Folder = Weapons_Folder:WaitForChild("Raiden")

--//Animation Folder Variables\\--
local Animations_Folder = script:WaitForChild("Animations")
local EquipAnim_Track = Animations_Folder:WaitForChild("Equip")
local SwordIdle_01_Track = Animations_Folder:WaitForChild("SwordIdle_01")
local SwordIdle_02_Track = Animations_Folder:WaitForChild("SwordIdle_02")

--//Player & Character Variables\\--
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local RootPart = Character:WaitForChild("HumanoidRootPart")
local LowerTorso, UpperTorso = Character:WaitForChild("LowerTorso"), Character:WaitForChild("UpperTorso")

local Equip_Value = Instance.new("BoolValue", Character)
Equip_Value.Value = false
Equip_Value.Name = "Equip"

--//Animation Variables\\--
local EquipAnim = Humanoid:LoadAnimation(EquipAnim_Track)
local SwordIdle_01 = Humanoid:LoadAnimation(SwordIdle_01_Track)
local SwordIdle_02 = Humanoid:LoadAnimation(SwordIdle_02_Track)

--Main
UserInputService.InputBegan:Connect(function(Input, GPE)
	if not Debounce then --To prevent the player from spamming
		Debounce = true
		
		--_HF Blade_--
		if Input.KeyCode == Enum.KeyCode.One and not GPE then
			if Equip_Value.Value == false then
				Equip_Value.Value = true
				EquipAnim:Play()
				
				Character.Sheath.HFBlade_Sheathed.Transparency = 1
				local WP_HFBlade = WPChar_Folder:WaitForChild("HFBlade"):Clone()
				WP_HFBlade.Parent = Character
				WP_HFBlade.CFrame = Character.RightHand.CFrame * CFrame.new(-0.257, -0.158, -1.55)
				
				local Weld = Instance.new("ManualWeld")
				Weld.Part0 = Character:WaitForChild("RightHand")
				Weld.Part1 = WP_HFBlade
				Weld.C0 = Weld.Part0.CFrame:ToObjectSpace(Weld.Part1.CFrame)
				Weld.Parent = Weld.Part1
				
				RootPart:WaitForChild("Blade_Equip"):Play()
				
				--Equipped Idle
				SwordIdle_01:Play()
				
				Humanoid.Running:Connect(function(Speed)
					local State = Humanoid:GetState()
					if (State ~=Enum.HumanoidStateType.Freefall) and (State ~=Enum.HumanoidStateType.Jumping) then
						if Speed == 0 then
							SwordIdle_01:Play()
							if SwordIdle_02.IsPlaying then
								SwordIdle_02:Stop()
							end
						else
							SwordIdle_01:Stop()
							SwordIdle_02:Play()
						end
					end
				end)
				
			elseif Equip_Value.Value == true then
				SwordIdle_01:Stop()
				SwordIdle_02:Stop()
				
				Equip_Value.Value = false
				EquipAnim:Play(0.100000001, 1, -1) --Reversed Animation Values: (Default Fadetime, Default Weight, Negative Speed)
				
				RootPart:WaitForChild("Blade_Unequip"):Play()
				wait(0.25)
				Character.Sheath.HFBlade_Sheathed.Transparency = 0
				local WP_HFBlade = Character:FindFirstChild("HFBlade")
				WP_HFBlade:Destroy()
			end
		end
		wait(0.1)
		Debounce = false
	end
end)

For your information, here is what the animations are for SworldIdle.


1 Like

Humanoid:LoadAnimation() is deprecated and instead you should load animations with the Animator Instance (which is parented to the humanoid).

so for example Humanoid:LoadAnimation(SwordIdle_01_Track) should be Humanoid:WaitForChild("Animator"):LoadAnimation(SwordIdle_01_Track)

if you are using a custom rig you might need to manually add the animator to the humanoid.

let me know if that does anything!

1 Like

Unfortunately, I’ve been receiving the same results. Yes, I have the Animator inside Humanoid.

1 Like

It’s best to play/change animations on the server, you can do this by firing a remote event to change the default animations of the character and changing the character’s humanoid state.

2 Likes

i definitely think the problem could also be the use of the Humanoid.Running event when you equip the sword.

the Humanoid.Running event will always fire. this means that the animations will play when the character moves even if the sword is unequipped.

you should disconnect the Humanoid.Running event using RBXScriptConnections when you unequip the sword to prevent this.

heres is an example of that in a nutshell to help you implement it:

local RunningConnection --Define the connection variable (this would be higher up in the actual script)


if Input.KeyCode == Enum.KeyCode.One and not GPE then
	if Equip_Value.Value == false then
		RunningConnection = Humanoid.Running:Connect(function(Speed) --Make the connection that listens out for if the humanoid is running
			local State = Humanoid:GetState()
			if (State ~=Enum.HumanoidStateType.Freefall) and (State ~=Enum.HumanoidStateType.Jumping) then
				if Speed == 0 then
					SwordIdle_01:Play()
					if SwordIdle_02.IsPlaying then
						SwordIdle_02:Stop()
					end
				else
					SwordIdle_01:Stop()
					SwordIdle_02:Play()
				end
			end
		end)

	elseif Equip_Value.Value == true then
		RunningConnection:Disconnect() --Disconnect the connection, which will kill it, stopping it from firing when the sword is unequipped
	end
end

again, let me know if this does anything!

2 Likes

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