Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Crouching animation pauses, but doesn't play?

I'm trying to make a crouching script that will play while the player is moving, and will pause while the player isn't. The animation is stuck being paused no matter what happens, and output shows no errors. Here's the script

Player = game.Players.LocalPlayer

repeat
	wait()
until Player.Character

local Mouse = Player:GetMouse()
local char = game.Workspace[Player.Name]
local crouching = false
local Animation = script.Anim
local animtrack = char.Humanoid:LoadAnimation(Animation)

game:GetService("UserInputService").InputBegan:Connect(function(input,proc)
	if proc == false and input.KeyCode == Enum.KeyCode.C and crouching == false then
	crouching = true
		animtrack:Play()
		char.Humanoid.WalkSpeed = 10
		if char.Humanoid.Running == false then
		animtrack:AdjustSpeed(0)
		elseif char.Humanoid.Running == true then
			animtrack:AdjustSpeed(1)
			end
	else
	if proc == false and input.KeyCode == Enum.KeyCode.C and crouching == true then
			crouching = false
		animtrack:Stop()
		char.Humanoid.WalkSpeed = 16
		end
	end
end)

Thanks if you can help!

2 comments

0

KeyDown is deprecated and use local variables please. This code isn't very well indented either.

0

That's not the problem, and doesn't help me at all... Even if it is deprecated, that section works.

1 answer

✓ Accepted answer

You were correct in somehow using Humanoid.Running to check when the humanoid moves, but that is not how you would do it. It's an event, so you would :Connect() it into a variable in your InputBegan, replacing the if.

You would create an empty local variable; local Event at the top of your script so the else can access it and disconnect it.

Event = Humanoid.Running:Connect(function(speed)
	if speed == 0 then
		animtrack:AdjustSpeed(0)
	else
		animtrack:AdjustSpeed(1)
	end
end)

and then, to disconnect the event, you would put this after animtrack:Stop():

Event:Disconnect()

This will prevent the Running event from firing any longer until you press C again.

The original problem with your script was that you were checking if Humanoid.Running was false, which it isn't, as it's a RBXScriptSignal, which was why it was only stopping the animation (I'm actually not sure why it was returning false). Your code was also only checking once, so if it did somehow work, it would run only once.

1 comment

0

Thanks a ton

This discussion is preserved as it was. New answers, comments, and votes are closed.