Walking and running anim

local UIS = game:GetService("UserInputService")
local ChangeSpeedEvent = game.ReplicatedStorage.ChangeSpeed
local localplayer = game.Players.LocalPlayer
--ONLY EDIT HERE
local runspeed = 20
local walkspeed = 16
--ONLY EDIT HERE
local running = false
local walking = false
local runninganimation = script.Running
local walkinganimation = script.Walking
UIS.InputBegan:connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift  then
		walking = false
		running = true
		if running == true then
		     local AnimationRunning = localplayer.Character.Humanoid:LoadAnimation(runninganimation)
			AnimationRunning:Play()
			ChangeSpeedEvent:FireServer(localplayer,runspeed)
		end
	end
end)
UIS.InputEnded:connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift  then
		running = false
		walking = true
		if walking == true then
			local AnimationWalking = localplayer.Character.Humanoid:LoadAnimation(walkinganimation)
			AnimationWalking:Play()
			ChangeSpeedEvent:FireServer(localplayer,walkspeed)
		end
	end
end)
UIS.InputBegan:connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.S then
		running = false
		walking = true
		if walking == true then
			local AnimationWalking = localplayer.Character.Humanoid:LoadAnimation(walkinganimation)
			AnimationWalking:Play()
			ChangeSpeedEvent:FireServer(localplayer,walkspeed)
		end
	end
end)

My problem: does not work as I want animation does not work sometimes.

1 Like

Using the Humanoid:LoadAnimation is not a best option because it was deprecated.

Instead do like this:

local UIS = game:GetService("UserInputService")
local ChangeSpeedEvent = game.ReplicatedStorage.ChangeSpeed
local localplayer = game.Players.LocalPlayer
--ONLY EDIT HERE
local runspeed = 20
local walkspeed = 16
--ONLY EDIT HERE
local running = false
local walking = false
local runninganimation = script.Running
local walkinganimation = script.Walking

-- Add the Humanoid Instance bro dont work yourself too hard
local Character = localplayer.Character
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
local Animator = Humanoid:WaitForChild("Animator") --// The thing that plays animation as they deprecated the old way of playing animation via Humanoid
UIS.InputBegan:connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift  then
		walking = false
		running = true
		if running == true then
			if Humanoid then --// Check if there's still a Humanoid
				local AnimationTrack = Animator:LoadAnimation(runninganimation)
				AnimationTrack:Play()
				ChangeSpeedEvent:FireServer(localplayer,runspeed)
			end
		end
	end
end)
UIS.InputEnded:connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift  then
		running = false
		walking = true
		if walking == true then
			if Humanoid then --// Check if there's still a Humanoid
				local AnimationTrack = Animator:LoadAnimation(walkinganimation)
				AnimationTrack:Play()
				ChangeSpeedEvent:FireServer(localplayer,walkspeed)
			end
		end
	end
end)
UIS.InputBegan:connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.S then
		running = false
		walking = true
		if walking == true then
			if Humanoid then --// Check if there's still a Humanoid
				local AnimationTrack = Animator:LoadAnimation(walkinganimation)
				AnimationTrack:Play()
				ChangeSpeedEvent:FireServer(localplayer,walkspeed)
			end
		end
	end
end)

I tried, but it still doesn’t work. Animations are not working now.

What device are you using? or is there some error says?

Oh sorry this error: Players.Dquvo.PlayerScripts.UserInputScript:15: attempt to index nil with ‘FindFirstChildWhichIsA’

try adding print inside the if statements, see if there’s an output.

Players.Dquvo.PlayerScripts.UserInputScript:15: attempt to index nil with ‘FindFirstChildWhichIsA’ I didn’t see it.

change the FindFirstChildWhichIsA into WaitForChild

This Error: Players.Dquvo.PlayerScripts.UserInputScript:15: attempt to index nil with ‘WaitForChild’

Imma try your code. and use it inside studio. ill be back

I fixed this error but again does not work as I want animation does not work sometimes. When i pressed A with S or W or D is working but doesn’t work when I press a button

local UIS = game:GetService("UserInputService")
local ChangeSpeedEvent = game.ReplicatedStorage.ChangeSpeed
local localplayer = game.Players.LocalPlayer
--ONLY EDIT HERE
local runspeed = 20
local walkspeed = 16
--ONLY EDIT HERE
local running = false
local walking = false
local runninganimation = script.Running
local walkinganimation = script.Walking

-- Add the Humanoid Instance bro dont work yourself too hard
localplayer.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator") --// The thing that plays animation as they deprecated the old way of playing animation via Humanoid
UIS.InputBegan:connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift  then
		walking = false
		running = true
		if running == true then
			if humanoid then --// Check if there's still a Humanoid
				local AnimationTrack = Animator:LoadAnimation(runninganimation)
				AnimationTrack:Play()
				ChangeSpeedEvent:FireServer(localplayer,runspeed)
			end
		end
	end
end)
UIS.InputEnded:connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift  then
		running = false
		walking = true
		if walking == true then
			if humanoid then --// Check if there's still a Humanoid
				local AnimationTrack = Animator:LoadAnimation(walkinganimation)
				AnimationTrack:Play()
				ChangeSpeedEvent:FireServer(localplayer,walkspeed)
			end
		end
	end
end)
UIS.InputBegan:connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.S then
		running = false
		walking = true
		if walking == true then
			if humanoid then --// Check if there's still a Humanoid
				local AnimationTrack = Animator:LoadAnimation(walkinganimation)
				AnimationTrack:Play()
				ChangeSpeedEvent:FireServer(localplayer,walkspeed)
			end
		end
	end
end)
end)

I think the problem here is when you’re running and suddenly pressed one of WASD the contents of it has running = false and walking = true, so the running animation stopped.

1 Like

So I have the tuned-up code of yours and got no errors.

LOCALSCRIPT: (can be found in StarterPlayer > StarterCharacterScript)

local UIS = game:GetService("UserInputService")
local ChangeSpeedEvent = game:GetService("ReplicatedStorage"):WaitForChild("ChangeSpeed") --// Use wait 
local LocalPlayer = game:GetService("Players").LocalPlayer
--ONLY EDIT HERE
local runspeed = 20
local walkspeed = 16
--ONLY EDIT HERE
local running = false
local walking = false
local runninganimation = script:WaitForChild("Running") -- use wait
local walkinganimation = script:WaitForChild("Walking") -- use wait

-- Add the Humanoid Instance bro dont work yourself too hard
local Character = LocalPlayer.Character
local Humanoid = Character:WaitForChild("Humanoid") -- no errors for me
local Animator = Humanoid:WaitForChild("Animator") --// The thing that plays animation as they deprecated the old way of playing animation via Humanoid


UIS.InputBegan:connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift  then
		walking = false
		running = true
		if running == true then
			if Humanoid then --// Check if there's still a Humanoid
				local AnimationTrack = Animator:LoadAnimation(runninganimation)
				AnimationTrack:Play()
				ChangeSpeedEvent:FireServer(runspeed)
			end
		end
	end
end)
UIS.InputEnded:connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift  then
		running = false
		walking = true
		if walking == true then
			if Humanoid then --// Check if there's still a Humanoid
				local AnimationTrack = Animator:LoadAnimation(walkinganimation)
				AnimationTrack:Play()
				ChangeSpeedEvent:FireServer(walkspeed)
			end
		end
	end
end)
UIS.InputBegan:connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.S then
		-- problem speculation starts here
		running = false
		walking = true
		if walking == true then
			if Humanoid then --// Check if there's still a Humanoid
				local AnimationTrack = Animator:LoadAnimation(walkinganimation)
				AnimationTrack:Play()
				ChangeSpeedEvent:FireServer(walkspeed)
			end
		end
	end
end)

SCRIPT (can be found in ServerScriptService)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeSpeedEvent = ReplicatedStorage:WaitForChild("ChangeSpeed")

ChangeSpeedEvent.OnServerEvent:Connect(function(Sender, speed)
	local Character = Sender.Character
	local Humanoid = Character:WaitForChild("Humanoid")
	
	if Humanoid then
		Humanoid.WalkSpeed = speed
	end
	
	warn("You fired the ChangeSpeedEvent!")
end)

Console output:

  • You fired the ChangeSpeedEvent! (36x)
2 Likes