Animation not playing on rig in viewport

Hello devs!

im trying to make a plugin and in order to make what plugin i want to create im trying to get an animation to play in a viewport frame but no matter what ive tried the plugin will not play the animation on the rig inside the viewport frame

heres the code if anyone knows how to fix it.

local Viewport = {}

function Viewport.Build(widget, animationId)
	local template = script:WaitForChild("ViewportFrame")
	local viewport = template:Clone()
	viewport.Name = "Viewport"
	viewport.Size = UDim2.fromScale(1, 1)
	viewport.Position = UDim2.fromScale(0, 0)
	viewport.BackgroundTransparency = 1
	viewport.AnchorPoint = Vector2.new(0, 0)
	viewport.Parent = widget

	local camera = template:FindFirstChildOfClass("Camera"):Clone()
	camera.Parent = viewport
	viewport.CurrentCamera = camera

	widget:GetPropertyChangedSignal("AbsoluteSize"):Connect(function()
		viewport.Size = UDim2.fromOffset(widget.AbsoluteSize.X, widget.AbsoluteSize.Y)
	end)

	local rigClone = template.Rig:Clone()
	local world = Instance.new("WorldModel")
	world.Parent = viewport
	rigClone.Parent = world

	for _, part in pairs(rigClone:GetDescendants()) do
		if part:IsA("BasePart") then
			part.Anchored = true
			part.CanCollide = false
		end
	end

	local animator = rigClone:FindFirstChildOfClass("Humanoid"):FindFirstChildOfClass("Animator") or Instance.new("Animator", rigClone.Humanoid)
	local animation = Instance.new("Animation")
	animation.AnimationId = animationId
	local track = animator:LoadAnimation(animation)
	track.Looped = true
	track:Play()

	local primary = rigClone.PrimaryPart or rigClone:FindFirstChildWhichIsA("BasePart")
	camera.CFrame = CFrame.new(primary.Position + Vector3.new(0, 2, 5), primary.Position)

	return viewport
end

return Viewport

I believe you have to use a WorldModel within a ViewportFrame in order to play animations.

oh ok ill try that and report back to you

how do i use worldmodels in my viewports?

You just have to add a WorldModel as a child of the ViewportFrame. The Model you’re animating must be a descendant of the WorldModel.

i just tried it and the rig still is not playing the correct animation

wait i forgot to update something in my script let me try it now

yeah no it didnt work any other ideas?

I don’t think it’s possible to animate Anchored parts. There’s no physics simulation (even under a WorldModel) in a ViewportFrame, so you should be able to safely unanchor all parts within the animated model.

everything in the rig is unanchored

You’re not still doing this?

for _, part in pairs(rigClone:GetDescendants()) do
	if part:IsA("BasePart") then
		part.Anchored = true
		part.CanCollide = false
	end
end

I can’t seem to find anything wrong with your code. I threw together this example to confirm a WorldModel is required to animate within a ViewportFrame. You could try altering this example with your target Animation and rig to possibly confirm it’s not an issue with either the Animation or rig.

AnimatedViewport.rbxm (5.1 KB)

the issue isnt that i cant ge the animation playing its the part where i cant get it to work in the plugin.

would you like me to send the full plugin?

To play the animation you need to create one anchored part (collision possibility false or true depends on collision possibility) then connect it to all parts/model together using “WeldConstrait”, also your “Rig” or whatever it is should be unachored either.

delete this part of code after you changed it*

	for _, part in pairs(rigClone:GetDescendants()) do
		if part:IsA("BasePart") then
			part.Anchored = true -- this is incorrect (your's anim/s will not be playing at all*
			part.CanCollide = false
		end
	end

i alreadly updated my code and it didnt seem to work..

Do you have an r6 or r15 rig?

Adaptive Animations beta feature might be causing bugs, try turning that off if your enrolled in beta features.

Have you added an anchored part inside it with weldconstraits?

Does the animation play if it’s in workspace