Animation doesn't show up immideatly after being changed trough script?
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.
01 | local UIS = game:GetService( "UserInputService" ) |
02 | local character = script.Parent |
03 | local humanoid = character:WaitForChild( "Humanoid" ) |
04 | local hrp = character:WaitForChild( "HumanoidRootPart" ) |
05 | local isCrouching = false |
06 | local defaultWalkSpeed = 16 |
07 | local defaultJumpPower = 13 |
11 | local StandToCrouchNoLegs = humanoid:LoadAnimation(script:WaitForChild( "StandToCrouchNoLegs" )) |
12 | local StandToCrouch = humanoid:LoadAnimation(script:WaitForChild( "StandToCrouch" )) |
13 | local CrouchWalkAnim = humanoid:LoadAnimation(script:WaitForChild( "CrouchWalkAnim" )) |
14 | local animateScript = character:WaitForChild( "Animate" ) |
15 | local defaultWalkAnim = animateScript.walk.WalkAnim.AnimationId |
16 | local defaultRunAnim = animateScript.run.RunAnim.AnimationId |
17 | local defaultIdleAnim 1 = animateScript.idle.Animation 1. AnimationId |
18 | local defaultIdleAnim 2 = animateScript.idle.Animation 2. AnimationId |
20 | local function crouch() |
22 | StandToCrouchNoLegs:Play() |
27 | while humanoid.WalkSpeed > 6 do |
28 | humanoid.WalkSpeed = humanoid.WalkSpeed - 1 |
31 | humanoid.JumpPower = 0 |
37 | animateScript.walk.WalkAnim.AnimationId = defaultWalkAnim |
38 | animateScript.run.RunAnim.AnimationId = defaultRunAnim |
39 | animateScript.idle.Animation 1. AnimationId = defaultIdleAnim 1 |
40 | animateScript.idle.Animation 2. AnimationId = defaultIdleAnim 2 |
41 | while humanoid.WalkSpeed < defaultWalkSpeed do |
42 | humanoid.WalkSpeed = humanoid.WalkSpeed + 1 |
45 | humanoid.JumpPower = defaultJumpPower |
48 | UIS.InputBegan:Connect( function (input, isTyping) |
49 | if input.KeyCode = = Enum.KeyCode.C and isTyping = = false then |
51 | if isCrouching = = false then |
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.