Crouch script wont work

I tried making an crouch script, that when a player presses a specific button, he crouches, and when he lets go, then he goes to standing up again.

the animation only plays once when I press the button even though I didn’t let go

Tried looping the animation but it was a bad idea since I made the animation from standing up to crouching, so if I loop it, he keeps standing up and down.

local plr = game.Players.LocalPlayer
local char = plr.Character

local hum = char:FindFirstChild("Humanoid")
local uis = game:GetService("UserInputService")

local Animator = hum:WaitForChild("Animator")

uis.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.S then
        local animation = Instance.new("Animation")
        local animDown = game.Workspace.Animations.crouch
        local animDownTrack = Animator:LoadAnimation(animDown)
        animDownTrack:Play()
        hum.WalkSpeed = 10
    end
end)

uis.InputEnded:Connect(function()
    local animation = Instance.new("Animation")
    local animDown = game.Workspace.Animations.crouch
    local animDownTrack = Animator:LoadAnimation(animDown)
    animDownTrack:Stop()
    hum.WalkSpeed = 16
end)

Is the animation the only problem?

Thats the only problem i found so far, why are you asking?

Well Just simply playing one animation, isn’t going to work as you want it to. I recommend making a function that tells if a player is moving or not and plays an idle or movement script, dependent on that.

I’ve done that before, so maybe I could help some
.
Also why aren’t you detecting if the input of the USI is a Enum.InputState.Begin

Don’t we just have to check if the player pressed a button, he just plays an animation that keeps going? Maybe we should seperate the start up of the animation (the player crouching from an standing state) and then play another crouch idle animation and keep the low walkspeed. Do you know how I can make that?

Also need all of that to reset after the player stops holding the button down.

So first I would play a separate animation for the character going down to crouch. After that, I would make a looping animation for idling/walking that would play until the player gets up again.

Tell me if you needs some example code for that

1 Like

Ok lets say we got the start up played after the player pressed the button, how can I start the other loop animation right after the start up animation stopped?

Here is where I need example code.

1 Like

Okay, give me a minute or two. I’ll do my best to give you a good example.

1 Like

Thanks a lot man, I appreciate your help!

1 Like

This is a local script right??

yea, its located in StarterCharacterScripts

1 Like

Sorry, taking longer than expected, I have to make a quick animation for it to work. Little longer wait

1 Like

Dont worry man, you helping me out is enough to wait for years!

1 Like

Ah, my short animations look weird but here is part of the script that will play the getting down animation, then play the looping one.

local plr = game.Players.LocalPlayer
local char = plr.Character

local hum = char:FindFirstChild("Humanoid")
local uis = game:GetService("UserInputService")

local Animator = hum:WaitForChild("Animator")

local CrouchAnim = script:WaitForChild("Crouch")
local CrouchIdleAnim = script:WaitForChild("CrouchIdle")
local CrouchMoveAnim = script:WaitForChild("CrouchMove")

local GetDownAnimation = Animator:LoadAnimation(CrouchAnim)--- Put in that animation Id in here
local LoopMoveAnimation = Animator:LoadAnimation(CrouchMoveAnim)--- Put in a crouching move animation id here
local LoopIdleAnimation = Animator:LoadAnimation(CrouchIdleAnim)--- Put in a Idle animation Id for crouching here

----Imagine your function here to begin the crouch

GetDownAnimation:Play()
GetDownAnimation.Stopped:Connect(function()
	LoopIdleAnimation:Play()
end)

Here is a video showing that loop and get-down animation, if you want to see it.
robloxapp-20220907-1751235.wmv (856.9 KB)

Heres my script as well
image

Walking animation too? Damn you helped me so much, I’ll go put that inside the game and I will tell you if it worked. Thanks again pal!

You’ll have to copy and paste that start animation to your current function. Also, you’ll have to add those animation instances into the script and put in an Id for them.

1 Like

Actually, I’m kinda tired, it’s 2am for me. I will complete it tomorrow. I’m sure it will work though, goodnight.

1 Like