Need help for crouching animation

So currently I’m working on movement set, and I have made this far until crouching animation.
What I want is to switch animation based on player’s walkSpeed. If the player is moving, the Movement animation will run. Otherwise, the Idle one will take place. However, when I stopped moving while crouching, the Movement still runs. I checked my Move detection code and it worked properly but the animation.

I actually found a solution for this before, using RenderStepped to track player’s moveDirection. But it comes with a problem: It worsen the game’s performance overtime. After that, I tried searching other solutions, and none of them works.

Here is my current script in StarterCharacterScripts

local run = game:GetService('RunService')
local UIS = game:GetService('UserInputService')
local tween = game:GetService('TweenService')
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character
local humanoid = character.Humanoid
local animator = humanoid:FindFirstChildOfClass("Animator")
local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
local timer

--numberical variables
local baseSpeed = 10
local sprintSpeed = 22
local crouchSpeed = 6

--boolean variables
local crouching = false
local runnable = false
local aiming, running

--setup
humanoid.CameraOffset = Vector3.new(0, .6, 0)
local tweenInfo = TweenInfo.new(0.3,Enum.EasingStyle.Quad, Enum.EasingDirection.Out,0)
local goal = {}

--animation
local crouch1 = Instance.new('Animation')
crouch1.AnimationId = "rbxassetid://6314087082"
local crouchIdle = humanoid:LoadAnimation(crouch1)

local crouch2 = Instance.new('Animation')
crouch2.AnimationId = "rbxassetid://7644044295"
local crouchAnim = humanoid:LoadAnimation(crouch2)

--codes
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and not crouching and not aiming then
			humanoid.WalkSpeed = sprintSpeed

--Crouching plays
	else if input.KeyCode == Enum.KeyCode.LeftControl then
			crouching = true
			humanoid.WalkSpeed = crouchSpeed
			goal.CameraOffset = Vector3.new(0,-1.8, 0)
			local crouchTween = tween:Create(humanoid, tweenInfo, goal)
			crouchTween:Play()
			humanoidRootPart.Size = Vector3.new(2,1.2,1)
--Determine which animation to play
			if running then
				crouchAnim:Play()
				crouchIdle:Stop()
			elseif not running then
				crouchAnim:Play()
				crouchIdle:Play()
			end
		end
	end
end)
UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		humanoid.WalkSpeed = baseSpeed
	else if input.KeyCode == Enum.KeyCode.LeftControl then
			crouching = false
			humanoid.WalkSpeed = baseSpeed
			goal.CameraOffset = Vector3.new(0,.6, 0)
			humanoidRootPart.Size = Vector3.new(2,2.1,1)
			local crouchTween = tween:Create(humanoid, tweenInfo, goal)
			crouchTween:Play()
			crouchIdle:Stop()
			crouchAnim:Stop()
		end
	end
end)

--Track player's speed
humanoid.Running:Connect(function(speed)
	if speed > 0 then
		running = true
	elseif speed == 0 then
		running = false
	end
end)

Output:
robloxapp-20220520-0922362.wmv (1.5 MB)

If you spot any mistakes or have any suggestions, please tell me. As an newcomer, I’m willing to learn. Thank you!

I can’t actually try this right now, but could you just change the player’s movement and idle animations while the player is crouching, so Roblox Studio’s built-in animate script will take care of that for you?
Again, this is just an idea, I would love to hear some feedback on this.

I tried this method too, but for some reason it doesn’t work

Really? Have you tried this?

I actually tried this before as for my custom walking and idling animation, then used InputService to detect key press but InputService wasn’t working in ServerScriptService at all (or at least I didn’t know how to script in ServerScriptService)

ServerScriptService is for serverscripts and inputservice only works when run on the client.

1 Like

I actually found the (temporary) solution for this. Instead of using 2 animations to switch, it is easier to change the “Crouch Animation” speed depending on Humanoid state by using AdjustSpeed for it.

local UIS = game:GetService('UserInputService')
local tween = game:GetService('TweenService')
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid
local animator = humanoid:FindFirstChildOfClass("Animator")

local crouch = Instance.new('Animation')
crouch.AnimationId = "rbxassetid://7644044295"
local crouchAnim = humanoid:LoadAnimation(crouch)

UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift and not crouching and not aiming and not gameProcessed then
			humanoid.WalkSpeed = sprintSpeed
--Crouching plays
	else if input.KeyCode == Enum.KeyCode.LeftControl and not gameProcessed then
			humanoid.WalkSpeed = crouchSpeed
			crouching = true
			goal.CameraOffset = Vector3.new(0,-1.8, 0)
			local crouchTween = tween:Create(humanoid, tweenInfo, goal)
			crouchTween:Play()
			humanoidRootPart.Size = Vector3.new(2,1.2,1)
--Corrected part
			humanoid.Running:Connect(function(speed)
				if speed > 0 then
					crouchAnim:AdjustSpeed(1)
				else
					crouchAnim:AdjustSpeed(0)
				end
			end)
			crouchAnim:Play()
		end
	end
end)

It worked like a champ at first though more tests are necessary. Took too long for me before I leanred AdjustSpeed was a thing, but better late than never. Thanks to anyone who helped me through this.