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 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.
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.
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