I've been trying to call an animation I made, and I have no idea how to do it. I checked the wiki and could not find ANY code on it. Does anybody know how to call/play an animation when a button is pressed?
I have this so far
local UserInputService = game:GetService("UserInputService") local function onInputBegan(input,gameProcessed) if input.UserInputType == Enum.UserInputType.Keyboard then local keyPressed = input.KeyCode if keyPressed == "Shift" then --animation end end end UserInputService.InputBegan:connect(onInputBegan)
Please don't forget to define player.
local player = game.Players.LocalPlayer
If there is already an animation file, or whatever you call it; you can do this.
if player.Character then -- to check if character exists local run = player.Character.Humanoid:LoadAnimation(// where the animation is) -- for example, "script.Run", you should get the idea if you know the fundamentals. wait() run:Play() player.Character.Humanoid.WalkSpeed = // walkspeed
for inputEnded, you can do the same; but the opposite.
player.Character.Humanoid.WalkSpeed = // walkspeed wait() run:Stop()
you can also make it a global variable (by removing the "local" on "local run", line 2), but I'm not sure which one would work, haven't tested this script myself.
If there isn't an animation file, create an instance by;
local run = instance.new("Animation") run.AnimationId = '// animation id' local playRun = player.Character.Humanoid:LoadAnimation(run) wait() playRun:Play()
if you have something without a Humanoid then use AnimationController instead and AnimationController can be used from a Script or a LocalScript
-- With a Humanoid -- LOCALSCRIPT local Players = game:GetService"Players" local Player = Players.LocalPlayer local Character = Player.Character or Player.CharacterAdded:Wait() local Humanoid = Character:WaitForChild"Humanoid" local function PlayAnimation() local Animation = Instance.new"Animation" -- if you haven't inserted one already Animation.AnimationId = "rbxassetid://1234567" -- Example animation id local AnimationTrack = Humanoid:LoadAnimation(Animation) AnimationTrack:Play() end
I hope this helps.