Search code examples
animationluamemberroblox

Why am I getting this error, when character is fully loaded?


I am trying to insert an animation into my code for the first time. Here is my code:

1   local player = game.Players.LocalPlayer
2   local char = player.Character or player.CharacterAdded:Wait()
3   local hum = char:WaitForChild("Humanoid")
4    
5   local animaInstance = Instance.new("Animation")
6   animaInstance.AnimationId = "rbxassetid://4641537766"
7    
8   local fireBallAnim = hum.LoadAnimation(animaInstance)
9   fireBallAnim:Play()

I am getting the error

The function LoadAnimation is not a member of Animation

I know the character has fully loaded, so I don't understand. Could I be getting this error if there is something wrong with the animation itself? What else am I missing out?

Thanks


Solution

  • local fireBallAnim = hum:LoadAnimation(animaInstance)
    

    This is a very confusing message for the error. The only problem is that you've called a member function with a . as opposed to a :. Switching it to a colon will fix your error.


    When you call a function on an object with a colon, it is automatically inserting the object as the first argument. A fun example of this can be seen with tables :

    -- insert an object into the table 't'
    local t = {}
    table.insert(t, 1)
    -- can also be written as...
    t.insert(t, 1)
    -- which is the same as...
    t:insert(1)
    

    All of these calls do the same thing. Calling the function with : is syntactic sugar for putting the t object as the first argument. So in your code, what's happening is you are calling LoadAnimation like this :

    local fireBallAnim = hum.LoadAnimation(<a humanoid object needs to go here>, <animation>)
    

    But since you are passing in the animation where the Humanoid is supposed to go, it is trying to find the LoadAnimation function on the animation object and failing.