I have a module script which helps load animations to the player Humanoid, but it keeps giving me this error:
19:11:48.530 ReplicatedStorage.AnimationScript:13: attempt to call a nil value - Client - AnimationScript:13
This is the function that receives it:
module.LoadAnimationsToHumanoid = function(parent, animationList)
if not animationList then
animationList = idList
end
local humanoid = parent:WaitForChild("Humanoid") -- Line 13
local animator = humanoid:WaitForChild("Animator")
local trackList = {}
local loadingAnimation = Instance.new("Animation")
for key, value in pairs(animationList) do
loadingAnimation.AnimationId = value
trackList[key] = animator:LoadAnimation(value)
end
return trackList
end
And this is the thing that calls it (Humanoid exists, it prints:)
local human = script.Parent:WaitForChild("Humanoid")
print(human)
local animator = human:WaitForChild("Animator")
local animationModule = require(game.ReplicatedStorage.AnimationScript)
local loadedAnimations = animationModule:LoadAnimationsToHumanoid(script.Parent, animationModule.AnimationIdList)
print(loadedAnimations)
What is the problem here? I hope it’s not replication, it’s getting called from a localScript…
local loadedAnimations = animationModule:LoadAnimationsToHumanoid(script.Parent, animationModule.AnimationIdList)
The colon is a shortcut for providing the ‘self’ keyword, so it is equivalent to calling the module function with ‘self’ added as the first arg. Expanded, it is equivalent to this:
local loadedAnimations = animationModule.LoadAnimationsToHumanoid(animationModule, script.Parent, animationModule.AnimationIdList)
But in your case, you don’t need the reference to ‘self’. So, try changing your call to the following (basically changing the colon to a dot):
local loadedAnimations = animationModule.LoadAnimationsToHumanoid(script.Parent, animationModule.AnimationIdList)
I’m not at home to test this… but maybe it will help based on what I remember.
EDIT: Changed the example for more clarity. Hopefully.
Yay! It passes! Thank you, I didn’t know what was causing the problem.
I will use . instead of : on module functions in the future.
All right, a bonus problem: It gives me an error of
18:54:03.592 Unable to cast value to Object - Client - AnimationScript:19
if not animationList then
animationList = idList
end
local humanoid = parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local trackList = {}
local loadingAnimation = Instance.new("Animation")
for key, value in pairs(animationList) do
loadingAnimation.AnimationId = value
trackList[key] = animator:LoadAnimation(value) -- Line 19
end
return trackList
end
return module```
Glad it got past that error! There are times you will want to use :, but that’s usually if you’re doing OOP.
Try modifying your code to load the Animation, not the AnimationId. I think it should look like this:
trackList[key] = animator:LoadAnimation(loadingAnimation) -- Line 19
Looks like your animation should replicate, since it’s a descendant of Humanoid. Was the Animator created on the server? Looks like it must in order to replicate. From the Animation page:
If an Animator is a descendant of a Humanoid or AnimationController in a Player’s Character then animations started on that Player’s client will be replicated to the server and other clients.
If the Animator is not a descendant of a player character, its animations must be loaded and started on the server to replicate.
The Animator object must be initially created on the server and replicated to clients for animation replication to work at all. If an Animator is created locally, then AnimationTracks loaded with that Animator will not replicate.