local animID = 14833871180
local human = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
local crouch = human:LoadAnimation(animID)
--detect if user presses C key
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.C then
print("ducked")
crouch:Play()
end
end)
It’s not printing or playing the animation. I don’t see why it won’t work.
1 Like
You’re setting your animations wrong, you have to use an animation Instance that has the animation id as a property. You’re also loading them on the Humanoid instead of the Animator.
Replace the first part (before the comment) with this:
-- Makes an instance and attaches the id to it
local animation = Instance.new("Animation")
animation.AnimationId = 'rbxassetid://14833871180' -- Ids must start with "rbxassetid://" before it, otherwise they wont load correctly
local human = game:GetService("Players").LocalPlayer.Character:WaitForChild("Humanoid")
local animator = human:WaitForChild("Animator")
local crouch = animator:LoadAnimation(animation)
I don’t think there should be any other issues apart from that? (forgot about the character stuff, oops) It works fine on my end with the changes mentioned. Make sure the script is in StarterPlayerScripts or StarterCharacterScripts
The print is not working, so it is not firing
Is the script inside of a LocalScript?
Yes it is in StarterPlayerScripts
Hello, the reason why it wasn’t working was because the code would error, breaking it. The error was because when it gets the player character, WaitForChild would error.
Edit: Another error was because there was no animation instance being put in Humanoid:LoadAnimation(), which would break the script.
Here is the fixed version:
local Players = game:GetService("Players")
local animID = 14833871180
local player = Players.LocalPlayer
repeat task.wait() until player.Character or player.CharacterAdded:Wait()
local character = player.Character
local human = character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://" .. animID
local crouch = human:LoadAnimation(Animation)
--detect if user presses C key
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.C then
print("ducked")
crouch:Play()
end
end)
1 Like
The code that you posted doesn’t seem to be structured well.
Here is the structured version of your code, which is inside of a LocalScript.;
local plr = game:GetService("Players").LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local human = chr:WaitForChild("Humanoid")
local userinputservice = game:GetService("UserInputService")
-- local animation = script:WaitForChild("Animation")
-- local animation_track = human.Animator:LoadAnimation(animation)
local bind = Enum.KeyCode.C
userinputservice.InputBegan:Connect(function(input)
if input.KeyCode == bind then
print("crouch")
-- animation_track:Play()
end
end)
If it prints the word crouch, then maybe use this code instead to play your animation…;
local plr = game:GetService("Players").LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local human = chr:WaitForChild("Humanoid")
local userinputservice = game:GetService("UserInputService")
local animation = script:WaitForChild("Anim")
local animation_track = human.Animator:LoadAnimation(animation)
local bind = Enum.KeyCode.C
userinputservice.InputBegan:Connect(function(input)
if input.KeyCode == bind then
print("crouch")
animation_track:Play()
end
end)
The character is probably not loaded by the time the script runs then? Please mention if there are any errors in the output.
You should use a character variable like local character = player.Character or player.CharacterAdded:Wait()
like the other replies have and check if that fixes it.
One last thing, your script will stop working properly after the first spawn since the character is only being set once at the start of the script (which means the variable + animations will still keep referencing the old, deleted character instead of the new one). I’d recommend moving the script to StarterCharacterScripts instead OR set up some events that listen when the player respawns (like Player.CharacterAdded)
to wait for child you wan to do:
local char = player.CharacterAdded:Wait()
repeat task.wait() until char.Parent == workspace
then get User Input Service.
It probably errored because Player.Character
is probably still nil
or non-existant yet, so use Player.CharacterAdded:Wait()
local animID = 14833871180
local character = if game.Players.Character and game.Players.Character.Parent then game.Players.Character else game.Players.CharacterAdded:Wait()
local human = character:WaitForChild("Humanoid")
local crouch = human:LoadAnimation(animID)
--detect if user presses C key
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.C then
print("ducked")
crouch:Play()
end
end)
You could try placing a LocalScript into ReplicatedFirst and typing (or copying) the following code:
local Players = game:GetService(“Players”)
local UserInputService = game:GetService(“UserInputService”)
local Player = Players.LocalPlayer or Players.PlayerAdded:Wait()
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild(“Humanoid”)
local Animator = Humanoid:WaitForChild(“Animator”)
local Animation = “Path To Animation”
local AnimationTrack = Animator:LoadAnimation(Animation)
UserInputService.InputBegan:Connect(function(input)
If input.KeyCode == Enum.KeyCode.F then
AnimationTrack:Play()
end
end)
UserInputService.InputEnded:Connect(function(input)
If input.KeyCode == Enum.KeyCode.F then
AnimationTrack:Stop()
end
end)