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
1I'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

01local UserInputService = game:GetService("UserInputService")
02 
03local function onInputBegan(input,gameProcessed)
04    if input.UserInputType == Enum.UserInputType.Keyboard then
05        local keyPressed = input.KeyCode
06        if keyPressed == "Shift" then
07            --animation
08        end
09    end
10end
11 
12UserInputService.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
1local player = game.Players.LocalPlayer

If there is already an animation file, or whatever you call it; you can do this.

1if player.Character then -- to check if character exists
2local run = player.Character.Humanoid:LoadAnimation(// where the animation is) -- for example, "script.Run", you should get the idea if you know the fundamentals.
3wait()
4run:Play()
5player.Character.Humanoid.WalkSpeed = // walkspeed

for inputEnded, you can do the same; but the opposite.

1player.Character.Humanoid.WalkSpeed = // walkspeed
2wait()
3run: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;

1local run = instance.new("Animation")
2run.AnimationId = '// animation id'
3local playRun = player.Character.Humanoid:LoadAnimation(run)
4wait()
5playRun: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

01-- With a Humanoid
02-- LOCALSCRIPT
03local Players = game:GetService"Players"
04local Player = Players.LocalPlayer
05local Character = Player.Character or Player.CharacterAdded:Wait()
06local Humanoid = Character:WaitForChild"Humanoid"
07 
08local function PlayAnimation()
09    local Animation = Instance.new"Animation" -- if you haven't inserted one already
10    Animation.AnimationId = "rbxassetid://1234567" -- Example animation id
11    local AnimationTrack = Humanoid:LoadAnimation(Animation)
12    AnimationTrack:Play()
13end

I hope this helps.

Answer this question