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

How do I run an animation with a script?

Asked by
kraigg -2
9 years ago

For example: I want to trigger an animation if someone clicks a Gui. How would I be able to perform this?

*Any example is fine, that was just an example so anyone can thoroughly comprehend my question, help is help! :)

1 answer

Log in to vote
1
Answered by 9 years ago

Use :LoadAnimation(). This is how it works.

local animation = script.Animation
local humanoid = workspace.Player1.Humanoid
local animation1 = humanoid:LoadAnimation(animation) --Load Animation is used.
animation1:Play() --Play the loaded animation. Must be loaded first.

Now we must use MouseButton1Click to see if the player clicked it.

local gui = script.Parent
gui.MouseButton1Click:connect(function()
    print("Hello World!")
end)

When you click it will print the message "Hello World!".


To load the animation you need a humanoid. To get a humanoid you need a character. To get a character you need a player. If the script is a LocalScript you could use a trick called LocalPlayer. This script will say the LocalPlayer's name.

local player = game.Players.LocalPlayer --You get LocalPlayer from game.Players.
print("Hello, "..player.Name.."!")

This will print "Hello, Player1!" if you were Player1.


Now we need to get the Character. This is easy. This script will print how much health the character has.

local player = game.Player.LocalPlayer --This script is local.
local character = player.Character --Character can be reached by the player even though the character is a descendant of workspace.

print(player.Name.." has "..character.Humanoid.Health.." health.") --We will use the humanoid in a later script!

If you spawn with full health you will get "Player1 has 100 health" if you were Player1.


Now we got the humanoid which is in the character. If we mix it all together we can get:

local anim = script.Animation
local gui = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid") --Find the humanoid

gui.MouseButton1Click:connect(function()
    if humanoid then --If the humanoid is real...
    local animation = humanoid:LoadAnimation(anim)
    animation:Play() --Custom animations made from players last 2 seconds long.
    end
end)

I hope this helped!


Also, you don't need an animation in the script, using Instance.new() you can create a new Animation Object.

local anim = Instance.new("Animation", script) --Instance.new() has 2 arguments. 1. The object 2. The object's parent.
anim.AnimationId = "http://www.roblox.com/asset/?id=ID OF THE ANIMATION GOES HERE!!!" --Animation ID goes here.
local gui = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid") --Find the humanoid

gui.MouseButton1Click:connect(function()
    if humanoid then --If the humanoid is real...
    local animation = humanoid:LoadAnimation(anim)
    animation:Play() --Custom animations made from players last 2 seconds long.
    end
end)
Ad

Answer this question