Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I am working on a dash script, I was wondering how animations work?

Asked by 6 years ago
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)

2 answers

Log in to vote
1
Answered by
Astralyst 389 Moderation Voter
6 years ago
Edited 6 years ago

Please don't forget to define player.

  • make sure script is a localscript
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()
0
also; make sure the animation is owned by you, or else it won't work and it'll send you an error saying "insufficient permission" or "unauthorized" or whatsoever, it just won't work if you don't own the animation. Astralyst 389 — 6y
Ad
Log in to vote
1
Answered by
Is_Hunter 152
6 years ago
Edited 6 years ago

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.

Answer this question