I am trying to run an animation in an NPC but I keep getting the same error, this is my code:
Wizard = {game.Workspace.ClassChoose.WizardSet.WizardDummy, 7, 15} function Choose(Class) if Class == "Mage" then local animController = Instance.new("AnimationController") animController.Parent = Wizard[1] local animTrack = animController:LoadAnimation("http://www.roblox.com/Asset?ID=371493384") animTrack:Play() Wizard[1].Tome.Book.Transparency = 0 Wizard[1].Tome.Pages.Transparency = 0 for i = 1, 10 do local Magic = game.Lighting.Magic:Clone() Magic.Position = CFrame.new(Wizard[0]["Left Arm"].Position + (math.random(5, 10)/100)) end end end Choose("Mage")
Why does this return this error:
23:02:36.087 - Unable to cast value to Object
23:02:36.087 - Script 'Players.Player.PlayerGui.LocalScript', Line 7 - global Choose
23:02:36.088 - Script 'Players.Player.PlayerGui.LocalScript', Line 19
23:02:36.088 - Stack End
Finally; I have used the array correctly, for some reason the array now counts from 1 in the first place not 0. Thanks.
EDIT: With what you have said lines 4-6 have been changed to:
local anim = Instance.new("Animation") anim.AnimationId = "http://www.roblox.com/Asset?ID=371493384" local animController = Instance.new("AnimationController") local animTrack = animController:LoadAnimation(anim) anim.Parent = Classes[Class][1] animTrack:Play()
But I am getting the same error.
According to the wiki page for LoadAnimation
, the first argument must be an Animation
object. The argument you passed to LoadAnimation
on line 6 is clearly a String
, not an Animation
.
Make an Animation
somewhere in your script and then pass that animation to LoadAnimation
. For example, you can put this on top of the script:
local anim = Instance.new("Animation") anim.AnimationId = "http://www.roblox.com/Asset?ID=371493384"
So that you can load it into the AnimationController
later:
local animTrack = animController:LoadAnimation(anim)
Oh! And after reading your error, it is worth noting that animating objects that are not players only works in a regular Script
, not a LocalScript
. Your script will only work in studio, not in a real server.
From the wiki page for Animation:
Non-Player Character animations should be played from a server Script.