Help with animation script

I have an issue with my animation LocalScript that is meant to control the moving and idle crawl animation.

ENABLED = nil
MOVING = false
CRAWL_KEY = Enum.KeyCode.C
-- move: 6579821142
-- idle: 6579822187

local GenInput = game:GetService("UserInputService")
local GenChar = script.Parent
local GenHumanoid = GenChar:WaitForChild("Humanoid")

local MoveAnimation = script:WaitForChild("crawl_move")
local IdleAnimation = script:WaitForChild("crawl_move")

local LoadMoveAnim = GenHumanoid:LoadAnimation(MoveAnimation)
local LoadIdleAnim = GenHumanoid:LoadAnimation(MoveAnimation)

GenInput.InputBegan:Connect(function(GenInput)
	if GenInput.KeyCode == CRAWL_KEY and ENABLED == false then
		LoadIdleAnim:Play()
		ENABLED = true
	else
		if ENABLED == true and (GenInput.KeyCode == CRAWL_KEY) then
			LoadIdleAnim:Stop()
			ENABLED = false
		end
	end
	if GenInput.KeyCode == Enum.KeyCode.A or GenInput.KeyCode == Enum.KeyCode.D and ENABLED == true then
		LoadMoveAnim:Play()
		MOVING = true
		ENABLED = true
	else
		if GenInput.KeyCode == Enum.KeyCode.A or GenInput.KeyCode == Enum.KeyCode.D and ENABLED == false then
			LoadMoveAnim:Stop()
			MOVING = false
			--ENABLED = false
		end
	end
end)


When I move to the right (D key) it moved as expected, but when I move to the left (A key) it always runs the crawl animation without me pressing C, and it continues like this until I press C and move to the right, then it happens again when I go left, etc.

Maybe try this for the InputBegan?

GenInput.InputBegan:Connect(function(GenInput)
	if GenInput.KeyCode == CRAWL_KEY then
		if not ENABLED then
			LoadIdleAnim:Play()
			ENABLED = true
		else
			LoadIdleAnim:Stop()
			ENABLED = false
		end
	elseif (GenInput.KeyCode == Enum.KeyCode.A or GenInput.KeyCode == Enum.KeyCode.D) then
		if ENABLED then
			LoadMoveAnim:Play()
			MOVING = true
			--ENABLED = true
		else
			LoadMoveAnim:Stop()
			MOVING = false
			--ENABLED = false
		end
	end
end)

Could be some conflictions with your statements

i think its cause of the if enabled == true part, maybe add a “not” or ~= statement, so you can check if the Crawl key is NOT pressed in the part where you do the thing with pressing A

Your code worked for my current issue, but when I press C again to stand up, I have to press C then move with A or D for it to fully work, is there any way to fix this?

Could be that the Move animation is still playing, in the code for when you pressed the crawl key and it is enabled, change this

LoadIdleAnim:Stop()
ENABLED = false

To this

LoadIdleAnim:Stop()
LoadMoveAnim:Stop()
ENABLED = false
2 Likes