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

How would I play multiple animations with one script to different NPCs?

Asked by 3 years ago

here is what I have so far

local NPC = game.Workspace.NPC

local function AnimationFunction()
    if NPC:FindFirstChild("StatueAnimation") then
        local animation = NPC:WaitForChild('StatueAnimation')
        local humanoid = NPC:WaitForChild('Humanoid')
        local StatueAnimation = humanoid:LoadAnimation(animation)
        StatueAnimation:Play()
        print("exampleanimation played")
elseif NPC:FindFirstChild("ExampleAnimation") then
            local animation = NPC:WaitForChild('ExampleAnimation')
            local humanoid = NPC:WaitForChild('Humanoid')
        local ExampleAnimation = humanoid:LoadAnimation(animation)
        ExampleAnimation:Play()
        print("exampleanimation played")
        else print("not working")
    end 
    end


for k,n in pairs(game.Workspace:GetChildren()) do


    if n.Name == "NPC" then
        AnimationFunction()

    end

end

1 answer

Log in to vote
1
Answered by 3 years ago

im PRETTY SURE this will work. If it doesnt, im sorry.

local function AnimationFunction(NPC)
        if NPC:FindFirstChild("StatueAnimation") then
            local animation = NPC:WaitForChild('StatueAnimation')
            local humanoid = NPC:WaitForChild('Humanoid')
            local StatueAnimation = humanoid:LoadAnimation(animation)
            StatueAnimation:Play()
            print("exampleanimation played")
    elseif NPC:FindFirstChild("ExampleAnimation") then
                local animation = NPC:WaitForChild('ExampleAnimation')
                local humanoid = NPC:WaitForChild('Humanoid')
            local ExampleAnimation = humanoid:LoadAnimation(animation)
            ExampleAnimation:Play()
            print("exampleanimation played")
        else
    warn("not working")
    end 
end


for k,n in pairs(game.Workspace:GetChildren()) do
     if n.Name == "NPC" then
            AnimationFunction(n)
    end
end

also sorry for horrid formatting

0
this did work, Thank you. And no, your code is very clean looking, much better than mine! astonplep 32 — 3y
Ad

Answer this question