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)
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)
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.
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)