Crouch script doesn't play the walk animation always, and fires even when typing c?

I have a crawl script that fires when typing c, and that doesn’t always use the crawl animation while in the crouch. Here is the script.

crawling = false
local humanoid = script.Parent.Humanoid
crawlAnim = nil
crawlAnimIdle = nil
game:GetService(“UserInputService”).InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.C then
if crawling == true then
crawling = false
crawlAnim:Stop()
crawlAnim:Destroy()
crawlAnimIdle:Stop()
crawlAnimIdle:Destroy()
humanoid.WalkSpeed = humanoid.WalkSpeed*2
humanoid.Parent.Head.Running.PlaybackSpeed = 1.85
else
crawling = true
crawlAnim = humanoid:LoadAnimation(script.CrawlAnim)
crawlAnimIdle = humanoid:LoadAnimation(script.CrawlAnimIdle)
humanoid.WalkSpeed = humanoid.WalkSpeed/2
humanoid.Parent.Head.Running.PlaybackSpeed = 1
end
end
end)

while wait()do
if crawling == true then
crawlAnim:AdjustSpeed(humanoid.WalkSpeed/8)
if humanoid.MoveDirection ~= Vector3.new(0,0,0)then
if crawlAnim.IsPlaying ~= true then
crawlAnim:Play()
end
else
crawlAnim:Stop()
crawlAnimIdle:Play()
end
end
end

If you put your script inside three of these " ``` " (Without the " ") then it will make a code block and it will be a lot easier to read. Also please make sure that the code is indented correctly!

"```"
print("Hello world!")
"```"
2 Likes

It’s also possible to:

[code]
[/code]

Hi there!

I’m not really sure what you mean when you say

however, the issue where you can crawl while typing is an easy fix!

You can use UserInputService:GetFocusedTextBox() to check if the player is typing in a text box before doing anything involving your crawling system.

As to your other issue, I think you’re referring to it doesn’t always play the idle/move animation when it’s supposed to? If your Humanoid.MoveDirection method isn’t working, you could try using either the Humanoid.Running event or the Humanoid.StateChanged event (however I’ve heard/found StateChanged doesn’t always work correctly, I’ve found .Running to be more reliable). Using this method, play your idle animation when the speed returned by the event is less than a small speed such as 1 (just in case your event happens to not return 0), and play your movement animation when the speed is greater than 1.

Good luck!

Does the animation have the correct priority?
https://developer.roblox.com/api-reference/property/AnimationTrack/Priority

Kind of confused with the exact sort of thing you want mainly because of the non-indented code which makes reading really annoying

Yes its all in the correct priority.

Just so I can find the actual issue can you send some steps of what is happening versus what should be happening. Example of what I mean:

What is happening:

  • One and one is fired
  • Three is returned

What should be happening:

  • One and one is fired
  • Two is returned

Just a quick example ^
Just can’t fully grasp the issue you’re having.

It’s firing because the input is ignoring key presses internally. That’s why the second argument GameProcessedEvent exists.

UserInputService.InputBegan:Connect(InputObject, GameProcessedEvent)
    if not GameProcessedEvent then
        -- Your code
    end
end)

Do this and you won’t experience your input actions running while a user is typing.

2 Likes