Animation not playing?

I’m attempting to make a crouch feature in my game, and I have an animation for crouching. However, when I test the game, it doesn’t play the animation, not even throwing an error or warning. I’ve even tried putting a wait() to wait for the animation to load, but to no avail.

local UIS = game:GetService("UserInputService")
local anim = script:WaitForChild("Animation")
local hum = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("Humanoid")

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.C or input.KeyCode == Enum.KeyCode.ButtonY then
		local anim2 = hum:LoadAnimation(anim)
		print("Y")
		anim2:AdjustSpeed(1)
		anim2:Play()
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.C or input.KeyCode == Enum.KeyCode.ButtonY then
		local anim2 = hum:LoadAnimation(anim)
		print("n")
		anim2:AdjustSpeed(-1)
		anim2:Play()
	end
end)

Your not loading the character in correctly, also, I recommend you get the character inside of the input connections so that this script will work when the character dies. Also, i think your trying to stop the animation on the input ended connection but your doing it incorrectly instead your script should look like this:

local UserInputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Animation = script:WaitForChild("Animation")
local LoadedAnim

UserInputService.InputBegan:Connect(function(Input)
	if (Input.KeyCode == Enum.KeyCode.C or Input.KeyCode == Enum.KeyCode.ButtonY) then
        local Character = Player.Character or Player.CharacterAdded:Wait()
        local Humanoid = Character:WaitForChild("Humanoid")
        LoadedAnim = Humanoid:LoadAnimation(Animation)
        LoadedAnim:Play()
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if (Input.KeyCode == Enum.KeyCode.C or Input.KeyCode == Enum.KeyCode.ButtonY) and (LoadedAnim) then
		LoadedAnim:Stop()
        LoadedAnim = nil
	end
end)


1 Like

Ok. With your given code, it works, but the player immediately gets back up. Any idea on how to fix this?
robloxapp-20200712-2202092.wmv (2.4 MB)

you want the player to hold the key or press it once to toggle on/off?

I want the player to toggle it.

Ok place this in startercharacter scripts in a local script instead for it to work properly, ensure you put the animation in the script

local UserInputService = game:GetService("UserInputService")

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("Animation"))

UserInputService.InputBegan:Connect(function(Input)
	if (Input.KeyCode == Enum.KeyCode.C or Input.KeyCode == Enum.KeyCode.ButtonY) then
        if (Animation.IsPlaying) then
            Animation:Stop()
        else
            Animtion:Play()
        end
	end
end)

I get the same issue where the player immediately gets back up.

Reopen the animation is the animation editor and ensure you have looped enabled and that the priority is set to action.

Now it just does this.
robloxapp-20200712-2220284.wmv (1.7 MB)

this looks like you did not animate it properly ensure that the pose is created at the 0 time interval.

I have put it there.

Delete the extra time stamps after 0, click on the white one, then right click and delete selected

Those posts are the keyframes for the character getting into position.

ok but its looped that’s why it looks like that, so your going to have to make another animation for while the character is down and set that one to looped, and then have one that is played to get the character in the pose.

So I can’t just use the .Stopped event of AnimaionTrack to stop the animation?

Animation:Play()
Animation.Stopped:Wait()
Animation:Stop()

No you need to make two animations since you want to show the character getting in the pose.

And how would that code look like? I’m pretty new to animations and such. Also sorry for asking so much.

Remember to disable the looped property on the animation where the character is getting into the pose.

local UserInputService = game:GetService("UserInputService")

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local StartingPoseAnimation = --put reference to animation where character goes into pose
local PoseAnimation = --put reference to the looped pose animation

StartingPoseAnimation = Humanoid:LoadAnimation(StartingPoseAnimation )
PoseAnimation = Humanoid:LoadAnimation(PoseAnimation)

UserInputService.InputBegan:Connect(function(Input)
	if (Input.KeyCode == Enum.KeyCode.C or Input.KeyCode == Enum.KeyCode.ButtonY) then
        if (PoseAnimation.IsPlaying) then
             PoseAnimation:Stop()
        else
            StartingPoseAnimation:Play()
            StartingPoseAnimation.Stopped:Wait()
            PoseAnimation:Play()
        end
	end
end)

I still get the same looping result, except now I can’t toggle it off.

I’m sorry i can’t help you now I’m going to bed, read this article on using animations https://developer.roblox.com/en-us/articles/using-animation-editor