Running animations for NPC through the Camera

-- NinjoOnline --

print(0)

local character = script.Parent
print(1)
local humanoid = character.Humanoid
print(3)

local animations = script:GetChildren()
print(4)

game:GetService('RunService').RenderStepped:Connect(function()
	print(0)
	local animation = animations[math.random(1, #animations)]
	if not animation then return end
	print(1)
	local loadAnimation = humanoid:LoadAnimation(animation)
	if not loadAnimation then return end
	print(2)
	loadAnimation:Play()
end)

Nothing in the script is running.

So what this is, it’s a model of an NPC. It has a Humanoid in it and this script is LocalScript. The NPC is stored inside ReplicatedStorage. Now the NPC gets cloned into the CurrentCamera, so it’s only visibile to the client when it needs to be visible. However, the LocalScript isn’t running at all. It is not Disabled.

Any help? Do scripts not run inside the Camera? How can I work around this? The model has to be Client based only!

1 Like

Objects inside the CurrentCamera is not supported. Cloning the NPC to workspace in a LocalScript would display the NPC only for the local player, and allow you to run your script in the same LocalScript that cloned it in the first place.

Edit: LocalScripts don’t run in the CurrentCamera anyways.

Ok, well I’ve now cloned it to the Workspace, but still no prints from it

Where is the LocalScript located at? Needs to be in one of the locations in the picture.

Oh, it was located inside the NPC character

If you clone the NPC to workspace in a LocalScript located in one of the locations, you now have the NPC variable in which you can control the NPC with.

But does the animations have to be run from the same script that cloned the NPC, or not?

Doesn’t matter as long as it’s a LocalScript and in a service it can run in- although it would help keep your code clean, it’s not required to be in the same script.

LoadAnimation requires the Animator object (Animator) to be a descendant of the game object

Get that error

Does your NPC have an animator inside of it? If so, make sure your NPC is parented to workspace before you play the animation.

No, I removed the script and put its content inside the same script that clones the model because the NPC isn’t one of the things listed that can run LocalScripts

I meant does it have an actual animator object inside of it?

No clue what that is but no. Just the parts and humanoid

Put an Animator inside the Humanoid, same error still occurs

Well, I don’t know much about what’s happening, sadly. However, I don’t know if it’s a great idea to load animations inside of RenderStepped. I’d assume they would just constantly spam the first frame or so.

How would I get the animations to contioulsy play then? The animation is an idle animation, so it needs to constantly be played.

Before doing loadAnimation:Play(), you can set loadAnimation.Looped to true.

There’s multiple animations though (3 idle animations) so they aren’t just doing the same thing over and over again.

The idea was that it would pick a random animation, play it, then pick another random one, and play that and just keep looping through like that.

EDIT Still doesnt play the animations at all (with the Looped set to true and removed the RenderStepped)

Ah, I see. Okay, something like this should work then:

function LoadIdleAnimation()
    local animation = animations[math.random(#animations)]
    if not animation then return end 

    local animTrack = humanoid:LoadAnimation(animation)
    animTrack.Stopped:Connect(function() LoadIdleAnimation() end)
    animTrack:Play()
end 

LoadIdleAnimation()

You should add states for the NPC if it ever does anything but idle, though. Otherwise, this will override any other animation you give it from another script. If it just idles, then this should be okay for doing a random one once one finishes.

As for the animations not working, maybe adding the Animator from the script might work. Though, I’m not too sure.

Instance.new("Animator", humanoid)