Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why can't I play this animation in this NPC? (Have checked other questions)

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.

1 comment

0

Arrays in Lua are 1-indexed, not 0-indexed like in other languages. The first element of an array starts at [1], not [0].

1 answer

✓ Accepted answer

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.

2 comments

0

So what lines of code am I wanting for lines 4 - 7, because with your 3 lines, animController isn't defined.

0

... You already defined animController at line 4. I'm not going to re-write the whole script. Just replace line 6 (local animTrack = animController:LoadAnimation("http://www.roblox.com/Asset?ID=371493384"))

This discussion is preserved as it was. New answers, comments, and votes are closed.