当角色完全加载时,为什么会出现此错误?

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

我第一次尝试在我的代码中插入动画。这是我的代码:

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()

我遇到错误

The function LoadAnimation is not a member of Animation

我知道角色已经满载了,所以没看懂。如果动画本身有问题,我会得到这个错误吗?我还错过了什么?

谢谢

local fireBallAnim = hum:LoadAnimation(animaInstance)

这是一条非常令人困惑的错误消息。唯一的问题是您使用 . 而不是 : 调用了成员函数。将其切换为冒号将修复您的错误。


当您使用冒号调用对象上的函数时,它会自动插入该对象作为第一个参数。一个有趣的例子可以在表格中看到:

-- 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)

所有这些调用都做同样的事情。使用 : 调用函数是将 t 对象作为第一个参数的语法糖。所以在你的代码中,发生的事情是你像这样调用 LoadAnimation :

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

但是由于您正在传递 Humanoid 应该去的动画,它试图在动画对象上找到 LoadAnimation 函数但失败了。