Adding animation to a player clone model in ViewportFrame?

Trying to add animation to player cloned model. Is it possible in a viewportframe?

local vpf = script.Parent:WaitForChild("ViewportFrame")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Workspace = game:GetService("Workspace")
local cam = Instance.new("Camera")
cam.FieldOfView = 70
vpf.CurrentCamera = cam
vpf.Visible = true

-- Clone the player's character
character.Archivable = true
local charClone = character:Clone()
character.Archivable = false
charClone.Parent = vpf

-- Set the clone's primary part to the center of the viewport
charClone:SetPrimaryPartCFrame(CFrame.new(0, 0, 0))

-- Add the idle animation
local function addIdleAnimation(clone)
	-- Find the humanoid of the cloned character
	local humanoid = clone:WaitForChild("Humanoid")
	-- http://www.roblox.com/asset/?id=507766388
	-- http://www.roblox.com/asset/?id=507766666
	-- Load an animation (replace with your animation asset ID)
	local idleAnimation = Instance.new("Animation")
	idleAnimation.AnimationId = "rbxassetid://507766388"  -- Replace with your own idle animation ID

	-- Load the animation into the humanoid
	local animTrack = humanoid:LoadAnimation(idleAnimation)

	-- Play the idle animation
	animTrack:Play()
	animTrack.Looped = true  -- Set the animation to loop indefinitely
end

-- Call function to add and play idle animation
addIdleAnimation(charClone)

-- Adjust the camera's orientation and rotate the character to face the camera
local function updateCamera()
	while true do
		-- Set the camera position and orientation relative to the cloned character
		-- Camera is placed at a fixed distance behind and above the character
		local offset = Vector3.new(0, 5, 10) -- Camera offset: (x, y, z) positions
		cam.CFrame = CFrame.new(charClone.PrimaryPart.Position + offset, charClone.PrimaryPart.Position)

		-- Rotate the character to face the camera
		local lookAtPosition = cam.CFrame.Position -- The camera's position
		local characterLookAt = CFrame.lookAt(charClone.PrimaryPart.Position, lookAtPosition)
		charClone:SetPrimaryPartCFrame(characterLookAt)

		wait(0.1) -- Update every 0.1 seconds
	end
end

-- Start camera update loop
updateCamera()
1 Like

Noticed you’re parenting the character model directly under the ViewportFrame, but to have animations you need to also add a WorldModel instance.

Setup should look like the one below vvv
image

WorldModel documentation vvv

Thank you!

1 Like

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