Hi there, thanks for checking.

Introduction

I'm trying to make a crouch feature in my game by changing the player's default Walk, Run, and Idle animation into a custom animation that I have made.

Problem

I managed to change the default animation, but the change is not visible if the player doesn't stop the currently playing animation. For example, if a player was to press the crouch button while running, the player would still be running with the normal animation until they release 'W' (or fall or anything as long as they stop the current animation), and only then if they press 'W' again would they use the crouch walking animation. Here is the script that I'm using.

local UIS = game:GetService("UserInputService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local hrp = character:WaitForChild("HumanoidRootPart")
local isCrouching = false
local defaultWalkSpeed = 16
local defaultJumpPower = 13

local deb = false

local StandToCrouchNoLegs = humanoid:LoadAnimation(script:WaitForChild("StandToCrouchNoLegs"))
local StandToCrouch = humanoid:LoadAnimation(script:WaitForChild("StandToCrouch"))
local CrouchWalkAnim = humanoid:LoadAnimation(script:WaitForChild("CrouchWalkAnim"))
local animateScript = character:WaitForChild("Animate")
local defaultWalkAnim = animateScript.walk.WalkAnim.AnimationId
local defaultRunAnim = animateScript.run.RunAnim.AnimationId
local defaultIdleAnim1 = animateScript.idle.Animation1.AnimationId
local defaultIdleAnim2 = animateScript.idle.Animation2.AnimationId

local function crouch()
	isCrouching = true
	StandToCrouchNoLegs:Play()
	animateScript.walk.WalkAnim.AnimationId = "rbxassetid://8125946204"
	animateScript.run.RunAnim.AnimationId = "rbxassetid://8125946204"
	animateScript.idle.Animation1.AnimationId = "rbxassetid://8141361463"
	animateScript.idle.Animation2.AnimationId = "rbxassetid://8141361463"
	while humanoid.WalkSpeed > 6 do
		humanoid.WalkSpeed = humanoid.WalkSpeed - 1
		wait(.1)
	end
	humanoid.JumpPower = 0
	
end

local function stand()
	isCrouching = false
	animateScript.walk.WalkAnim.AnimationId = defaultWalkAnim
	animateScript.run.RunAnim.AnimationId = defaultRunAnim
	animateScript.idle.Animation1.AnimationId = defaultIdleAnim1
	animateScript.idle.Animation2.AnimationId = defaultIdleAnim2
	while humanoid.WalkSpeed < defaultWalkSpeed do
		humanoid.WalkSpeed = humanoid.WalkSpeed + 1
		wait(.1)
	end
	humanoid.JumpPower = defaultJumpPower
end

UIS.InputBegan:Connect(function(input, isTyping)
	if input.KeyCode == Enum.KeyCode.C and isTyping == false then
		if deb == false then
			if isCrouching == false then
				deb = true
				crouch()
				wait(.2)
				deb = false
			else
				deb = true
				stand()
				wait(.2)
				deb = false
			end		
		end
	end
end)

I couldn't come up with anything to solve this problem. I imagine it would require me to Reload animation (if that exist). Anyway, thank you for reading, a help would be greatly appreciated.

4 comments

0

One method I could come up with is changing humanoid's state, this will update his animations, the shortest lasting state is Landed but since it does not last 0 seconds you will see little animation for 0.1 seconds there, here is the code: https://gist.github.com/kirdaaa/2f04c1448e83d8c0a36ffe9b9b967346

0

A second solution does not require modifying the script, if in animation editor you make that the animation animates ALL the body parts, it will override any other animations, see here: https://i.imgur.com/1goW0d9.png (look at the -0.001 transformations, this is so it thinks that it's animation, if it would be 0 it would be ignored, you need to do that for every limb)

0

I think i did animated all body parts, but i will check it again.

0

Btw there's no need for stand to crouch anim to be stopped because it's only an animation that i made to transition from standing to crouching and it doesn't loop