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

NPCs do not load animation?

Whenever I try to load an animation into an NPC, it says "Object must be in Workspace before loading animation."This is the script( which is in the npc model)

hitt = false
function Hit(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if not hitt and hit.Parent.Name ~= script.Parent.Name and script.Parent:FindFirstChild("Left Arm") and script.Parent:FindFirstChild("Right Arm") and human and hit.Parent:FindFirstChild("IsSoldier") and hit.Parent.IsSoldier.Value == true then
		hitt = true
			local AnimController = Instance.new("AnimationController",script.Parent)
			local animation = Instance.new("Animation",script.Parent.Humanoid)
			animation.AnimationId = "http://www.roblox.com/asset?ID=359602146"
			local animTrack = AnimController:LoadAnimation(animation)
			animTrack:Play() -- play animation
		human:TakeDamage(math.random(20,30))
		wait(.8)
		animation:Destroy()
		AnimController:Destroy()
		hitt = false
	end
end


script.Parent["Left Arm"].Touched:connect(Hit)
script.Parent["Right Arm"].Touched:connect(Hit)

As you can see, the animation is created in the script and placed into the humanoid. P.S. The model usually attacks someone(using this script) shortly after it is cloned from the serverstorage to the workspace(about 1 second)

1 answer

✓ Accepted answer

You are Parenting the "Animation" Instance to the player you don't do that, you can add the

local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset?ID=359602146"

ontop of the function then

have the Animation Controller be in the Humanoid

local AnimController = Instance.new("AnimationController",script.Parent.Humanoid)

Also one more thing you must make sure you have a HumanoidRootPart that has all the parts connected together with a Motor6D

To learn more about Animations there is a helpful wiki post here:

http://wiki.roblox.com/index.php?title=Animations

1 comment

0

Thanks! I figured out that there was no humanoidrootpart, thanks anyways!

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