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

How do I detect if an animation has been competed?

Asked by
rdkv 12
4 years ago

I have a reload animation that plays on KeyDown, but I need to find out how to know when an animation has been fired and finished. Playing the animation and waiting a few seconds then stopping it isn't enough, I need to know exactly when the keyframes are finished operating.

Thanks,

ReloadAnim = Humanoid:LoadAnimation(script.Parent.ReloadR6)

script.Parent.Equipped:Connect(function()
    Mouse.KeyDown:Connect(function(key)
        if key == "r" then
            Reloading = true
            ReloadAnim:Play()
        end
    end)
end)

2 answers

Log in to vote
0
Answered by
iuclds 720 Moderation Voter
4 years ago
Edited 4 years ago
ReloadAnim.Stopped:Wait()
print('i like boneless burritos')

Or you can do

ReloadAnim.Stopped:Connect(function()
    print('i like cheeseless pizza')
end)

There is an event in an animation base called "Stopped"

By the way, Mouse.KeyDown is deprecated / not recommended Use UserInputService. it's a better way to get a key pressed

Ad
Log in to vote
0
Answered by
RBLXNogin 187
4 years ago

Hello there! This is a very interesting question and prooves that you are getting further into the world of ROBLOX Development. There are several methods inorder to accomplish this goal.If you are trying to Stop an animation, you must stop it from the humanoid. Additionally, please note that :Stop() are not methods. There are several solutions. First is the most basic solution: An Event.

An event is a piece of code that recieves an execution when a certain action is met, or when something becomes fired. An example of this is PlayerAdded, which is an event which is executed when a client connects to the server, basically when a new user joins the game. Roblox provides us with RBXScriptSignals (aka events) which are fired whenever an event take place Events can go in several ways, example:

game.Players.PlayerAdded:Connect(function()
   --code for when event executed goes here
end)

or

function color
    --code
end
game.Players.PlayerAdded:Connect(color)

Basically events are useful in many ways. In our scenario, we will use an event known as Stopped.

Example:

ReloadAnim.Stopped:Connect(function()
    print("The animation has stopped playing.")
    --code
end)

You can control what happens when the event is executed. In this scenario, the event is when the animation stops, and we can control the code and how we use it. .Stopped is an event we can use for activationg a set of code for when an animation has stopped playing.

Secondly, you can make the program continue on a wait until the animation stopped. Making the application(or game) wait and pause until the animation is done playing can be a significant method. Here is how you will accomplish it:

ReloadAnim.Stopped:Wait()
print("The animation is done executing")

These methods are not only super accessible but also provide functionality and can be useful for controlling what happens after an animation is done executing. Don't forget these are only basic solutions. There are several more advanced options that the more efficient developers use. Explore wikia, developerhubs, and even youtube for more powerful solutions!

Here is my final example on how ScriptingHelpers user,Internal_1, has created a method that allows him to call a "StopAnimations" method which will immediately stop the animations being played, and is extremely unique.

local Animations = script.Animations:GetChildren() -- Gets the animations

function stopAnimations()
    local playing = game.Players.LocalPlayer.Character.Humanoid:GetPlayingAnimationTracks()
    for _,Anim1 in pairs(playing) do
    for _,Anim2 in pairs(Animations) do
           if Anim1.Animation == Anim2 then
            Anim1:Stop()
                break
           end
        end
    end
end

stopAnimations() -- Fires the function

There you have it. This is the solution to your problem in debt. You get your knowledge, I get my scriptinghelpers rep and a checkmark on this awnser, and it all works out in the end :)

Answer this question