Idle animation not playing

Hey! I’m trying to load a NPC into a viewport frame, and then make it pose. I went about this by making an idle animation, and it doesn’t seem to be playing. I’m pretty bad with anything animation so it might be something little I overlooked. Here is my code:

local repStorage = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer

for i, frame in pairs(script.Parent:GetChildren()) do
	if frame:IsA("Frame") and frame.Tower.Value ~= "None" then
		local model = repStorage.Towers[frame.Tower.Value]:Clone()
		model:PivotTo(CFrame.new(0, 0, 0))
		model.Parent = frame.ViewportFrame
		
		local animation = model.Humanoid:LoadAnimation(model.ViewportAnimation)
		animation:Play()
		
		local viewportCamera = Instance.new("Camera", frame.ViewportFrame)
		frame.ViewportFrame.CurrentCamera = viewportCamera

		viewportCamera.CFrame = CFrame.new(Vector3.new(0, 3, 4))
	end
end
2 Likes

First off I revamped your code:

Revamped Code
-- Revamped Code

local repStorage = game:GetService("ReplicatedStorage")
local towers = repStorage:WaitForChild('Towers', 10)

local player = game:GetService("Players").LocalPlayer

local parent = script.Parent

for _, frame in ipairs(parent:GetDescendants()) do
	if frame:IsA('Frame') then
		local frameTower = frame:WaitForChild('Tower', 10) :: StringValue
		local viewportFrame = frame:WaitForChild('ViewportFrame', 10) :: ViewportFrame
		
		if frameTower and viewportFrame and frameTower.Value ~= "None" then
			
			local model = towers:WaitForChild(frameTower.Value, 10):Clone()
			model:PivotTo(CFrame.new(0, 0, 0))
			model.Parent = viewportFrame
			
			local viewPortAnimation = model:WaitForChild('ViewportAnimation', 10)
			
			local animation = model.Humanoid:LoadAnimation(viewPortAnimation, 10)
			animation:Play()

			local viewportCamera = Instance.new("Camera", viewportFrame)
			viewportFrame.CurrentCamera = viewportCamera

			viewportCamera.CFrame = CFrame.new(0, 3, 4) -- I changed Vector3.new(0,3,4) to this because they do the same thing
		end
	end
end

And second, could you show me how your files are placed?

1 Like

I’m not sure what you mean by my “files,” but if you mean the explorer then I can show you. I also forgot to mention also that it gives no errors in the output. All children of TowersFrame have the same children under them. (Besides script)

Screen Shot 2023-09-29 at 6.10.15 PM

1 Like

Yeah, I forgot the name :sweat_smile:. And is the rig at least showing up? And is the animation looped?

1 Like

The rig is showing up in-game, I don’t think the animation is looped though. The animation doesn’t appear to have a property that’s called Looped?

Screen Shot 2023-09-29 at 6.24.03 PM

1 Like

There is a looped property, for example:

local anim = hum:LoadAnimation(animation)
anim.Looped = true

anim:Play()
1 Like

try placing a worldmodel into the viewportframe and then cloning the npc into the worldmodel, from what i read worldmodels have the ability to play animations within viewports.

1 Like

Just tested out @KingBlueDash answer, and he’s right.

1 Like

Yeah, I still really appreciate the help though! Thank you @KingBlueDash for the solution!

It’s fine if not, but do you know why this line isn’t rotating the NPC 180 degrees? I found out he was backwards and this line of code isn’t working.

model:PivotTo(CFrame.new(0, 0, 0), CFrame.Angles(0, 180, 0)
2 Likes

Glad i could help (character limit.)

1 Like

you can try this

model:PivotTo(CFrame.new(), CFrame.Angles(0, math.rad(180), 0))

CFrame.new() is the same as CFrame.new(0, 0, 0)

CFrame.Angles uses math.rad() to rotate if its not 0.

1 Like

Just do:

local pivot = model:GetPivot()
model:PivotTo(pivot * CFrame.Angles(0, math.rad(180), 0))
3 Likes

@kyler_josiah it worked! Thank you so much @/kyler_josiah and @KingBlueDash! :grin:

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.