Crouch System Problems

  1. What do you want to achieve? I’m just trying to make a simple crouch system with an idle and a walk animation. (The crouch is activated if you’re holding C.)

  2. What is the issue? I can’t figure out how to make it so that a crouched idle plays while you’re standing still, and a crouched walk plays when you’re moving.

  3. What solutions have you tried so far? I’ve tried looking at many different devforum posts, but I can’t find anything that helps.

Here is the code I’m using currently. I’m not sure how to edit it so that it includes the mechanics I need.

local contextActionService = game:GetService("ContextActionService")

local character = script.Parent

local animation = Instance.new("Animation", script)
animation.AnimationId = "rbxassetid://12460722607"

local animationLoad = character:WaitForChild("Humanoid"):LoadAnimation(animation)

local isCrouching = false

local function crouchFunction()
	if isCrouching == false then
		isCrouching = true
		animationLoad:Play()
		character.Humanoid.WalkSpeed = 8
	else
		animationLoad:Stop()
		isCrouching = false
		character.Humanoid.WalkSpeed = 16
	end
end

contextActionService:BindAction("Crouch", crouchFunction, true, Enum.KeyCode.C)

(This is in a local script in StartCharacterScripts, by the way.)

I would appreciate any help fixing this so I can continue work on my game. Thanks.

local function crouchFunction(actionName, inputState, inputObject)
     if inputState == Enum.UserInputState.Begin and inputObject == Enum.KeyCode.C then
	    -- continue your code here
     end
end

contextActionService:BindAction("Crouch", crouchFunction, true, Enum.KeyCode.C)

You can check the humanoid walk speed. If it’s set to 0 then you aren’t moving and if you are moving you can check if the humanoid walkspeed is >=1.

I may be doing something wrong, but if the player stops moving, their walk speed isn’t set to 0, so this doesn’t really work. As far as I know, the walk speed of the humanoid is just how fast the player is able to move, not how fast they’re moving in the present moment.

Try something like MoveDirection ``local Player = game.Players.LocalPlayer --local player
repeat wait(1) until Player.Character --repeats wait(1) until the character has loaded
local Character = Player.Character --character

local Humanoid = Character:FindFirstChild(“Humanoid”) --finds the humanoid from the players character

local RunService = game:GetService(“RunService”) --run service

local function IsPlayerMoving() --function
if Humanoid.MoveDirection.Magnitude == 0 then --if the movedirection is equal to 0 then the following will happen;
print(“Player is not moving”)
elseif Humanoid.MoveDirection.Magnitude > 0 then --if the movedirection is greater than 0 then the following will happen;
print(“Player is moving”)
end
end

RunService.RenderStepped:Connect(IsPlayerMoving) --running the function ``

You can use the MoveDirection property to check if it’s moving or not for some reason the code im putting in is not embedding.