Home > Software design >  Why is my Animation Not Working in Roblox Studio
Why is my Animation Not Working in Roblox Studio

Time:10-04

So I followed this one DevKing tutorial to make an animation and for some reason I cant get it to work, because I got this error: attempt to index nil with 'Humanoid'

This is my code:

local player = game.Players.LocalPlayer
local char = player.Character
local Hum = char.Humanoid
local ArmAnim = script.ArmAnim
local ArmTrack = Hum:LoadAnimation(ArmAnim)
ArmTrack:Play()

I have already tried rereading all of my code and looking back at the video but I cant seem to get it to work.

CodePudding user response:

Your code should work just fine, but you have a timing issue in your code. Based on the comments in the question, since this LocalScript is in StarterPlayerScripts, this script will fire as soon as a player joins. But, when a player first joins the server, their character is not immediately spawned into the world. So there is a short time period where player.Character is nil.

So to fix this issue, you need to wait until the character is loaded into the world.

You can do this in a few different ways.

  1. Move this LocalScript into StarterCharacterScripts. This will change when the script fires to after the character is loaded into the world.

or

  1. Alter your LocalScript to wait for the Humanoid to exist.
local player = game.Players.LocalPlayer
local char = player.Character

-- if the player hasn't loaded yet, wait for the character to spawn into the world
if char == nil then
    player.CharacterAdded:Wait()
    char = player.Character
end

local hum = char.Humanoid
local ArmAnim = script.ArmAnim
local ArmTrack = hum:LoadAnimation(ArmAnim)
ArmTrack:Play()

CodePudding user response:

Humanoid is a child of the Character property (a Model instance). Not a property.

Try local Hum = char:WaitForChild("Humanoid")

https://developer.roblox.com/en-us/api-reference/class/Model

https://developer.roblox.com/en-us/api-reference/class/Humanoid

Please read the manual. It even comes with an example of how to get the Humanoid instance from a Model.

  • Related