Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

NPCs do not load animation?

Asked by 8 years ago

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

Log in to vote
1
Answered by 8 years ago

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

0
Thanks! I figured out that there was no humanoidrootpart, thanks anyways! laughablehaha 494 — 8y
Ad

Answer this question