Press key to activate animation?

Well my problem is that when pressing the “C” on the keyboard, the following script detects it, and then changes a stringValue to true, and that true should be detected by activating the crouch animation, if it changes to true but does not do the animation, and pressing again does not change the value to false either.

local UserInput = game:GetService("UserInputService")
local Running = false
local WalkSpeed = game.Players.LocalPlayer.PlayerGui.Configuracion:WaitForChild("WalkSpeed").Value
local Player = script.Parent
local take = script:WaitForChild("Take")
local Crouch = game:WaitForChild("ReplicatedStorage"):WaitForChild("JuegoConfg"):WaitForChild("Crouch")
local Crouchidle = game:WaitForChild("ReplicatedStorage"):WaitForChild("JuegoConfg"):WaitForChild("Crouchidle")

game.Players.LocalPlayer.PlayerGui.Configuracion.WalkSpeed:GetPropertyChangedSignal("Value"):Connect(function()
	WalkSpeed = game.Players.LocalPlayer.PlayerGui.Configuracion.WalkSpeed.Value
end)

local KeyText = "C"

local AgacharseKey = game.Players.LocalPlayer:WaitForChild("Keys"):WaitForChild("AgacharseKey")

AgacharseKey:GetPropertyChangedSignal("Value"):Connect(function()
	KeyText = AgacharseKey.Value
end)

UserInput.InputBegan:Connect(function(key)
	if (key.KeyCode.Name == KeyText) then
		if not Running then
			Running = true
			--poner animacion
			if take.Value == 'false' and script.Anim.Value == 'false' then
				script.Anim.Value = 'true'
				take.Value = 'true'
				local flip = Crouch:Clone()
				flip.Parent = script
				local animloader2 = script.Parent:WaitForChild("Humanoid"):LoadAnimation(flip)
				animloader2:Play()
		else
				while (1) do
					--desactivar animacion
					wait(0.1)
				if script.Anim.Value == 'false' then
					animloader2:Stop()
					flip:Destroy()
					end
				end
			end
		end
	end
end)

Are you changing the value in a LocalScript? After that, is it being picked up by a Server Script?

ALSO this function is deprecated. You should be doing
Animator:LoadAnimation

No, everything is done by the LocalScript that I put earlier.

1 Like

This article is also helpful:

To reitterate though, your problem is that the function you are using to Load the animation is deprecated, you need to be using “Animator:LoadAnimation”. The Animator is a child of the Humanoid, so you can just add in :WaitForChild("Animator")

1 Like

I think that’s not the problem though, it’s deprecated but still works (he should change it though).
Are there any errors?

.Name is not a property of the KeyCode object

Just change that to:

if (key.KeyCode == Enum.KeyCode.C) then

You should be using Enums as well

1 Like

Yes, but I can’t fix them, it has yellow lines under where it says “Else”

1 Like

You have problems there because you forgot to include an end statement. Go back through your script and make sure that you have included “end” to end functions and if statements.

1 Like

It should look like this:

			if take.Value == 'false' and script.Anim.Value == 'false' then
				script.Anim.Value = 'true'
				take.Value = 'true'
				local flip = Crouch:Clone()
				flip.Parent = script
				local animloader2 = script.Parent:WaitForChild("Humanoid"):LoadAnimation(flip)
				animloader2:Play()
			end
		else
2 Likes