i want to code a system where if you press a key, the player will lift up his hands until you let go and then it will return to its original state. ive tried many different ways that i thought could work but none worked. how would i actually do this?
You should use UserInputService, and then run the animation through the humanoid’s animator. It would probably look SOMETHING like this in a local script:
local UIS = game.GetService("UserInputService")
local liftAnimation = Instance.new("Animation")
local liftAnimation.AnimationId = -- enter roblox id
local player = game.Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local playerAnimator = humanoid:WaitForChild("Animator")
local liftAnimationTrack = playerAnimator:loadAnimation(liftAnimation)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then -- replace E with whatever key you want
liftAnimationTrack:Play()
end
end)
I hope this helps. Good luck, and feel free to ask questions.
Your code is not perfect I’d have to say.
So basically you have to do some edit to your code, making the animation track looped, since lifting hands seems like a looped action.
So the code should be something like:
--load animation with looped being true--
UIS.InputBegan:Connect(function(input,gpe)
if gpe or input.UserInputState ~= Enum.UserInputState.Begin then return end
if input.KeyCode == Enum.KeyCode.E then
Animation:Play()
end
end
UIS.InputEnded:Connect(function()
if Animation then Animation:Stop(0.5) end --I recommend having a fade time
end
i have a problem with both iterations of the code as of rn. @JustCrock 's code played the animation correctly, but didnt keep the animation playing until i let go of the key. your code gives me the error: Stop is not a valid member of Animation “Animation”. here is my code again for reference
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://15304871151" -- enter roblox id
local character = player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local playerAnimator = humanoid:WaitForChild("Animator")
local liftAnimationTrack = playerAnimator:loadAnimation(Animation)
UIS.InputBegan:Connect(function(input,gpe)
if gpe or input.UserInputState ~= Enum.UserInputState.Begin then return end
if input.KeyCode == Enum.KeyCode.E then
Animation:Play()
end
end)
UIS.InputEnded:Connect(function()
if Animation then Animation:Stop(0.5) end --I recommend having a fade time
end)
i figured out the error code, the :Play() and :Stop() events were being called on the animation itself, not the track. the code is now working. thanks for your help.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.