Copy Animations from Characters in Workspace to their Clones in Viewport Frame

I been working on a script that uses a viewport frame to create an x-ray of all characters in the workspace, while the characters do correctly clone to the viewport frame and follow the original, they don’t copy the same animations. I’m still new to Roblox Studio in general and dont know how to copy animations from the original character to the cloned ones.

Here’s the full script:

local viewport = script.Parent
viewport.Visible = false
local viewCam = Instance.new("Camera")
viewCam.Parent = workspace
viewport.CurrentCamera = viewCam


--Makes viewport camera follow the workspace camera
game:GetService("RunService").RenderStepped:Connect(function()
	viewCam.CFrame = workspace.CurrentCamera.CFrame
end)

--Just gives me a way to toggle the viewport frame
game:GetService("UserInputService").InputBegan:Connect(function(io, gpe)
	if io.KeyCode == Enum.KeyCode.F then
		viewport.Visible = true
	end
end)
game:GetService("UserInputService").InputEnded:Connect(function(io, gpe)
	if io.KeyCode == Enum.KeyCode.F then
		viewport.Visible = false
	end
end)

for _, part in workspace:GetChildren() do
	if part:IsA("Model") then
		if part:FindFirstChildOfClass("Humanoid") then
			--THis clone the orginal character and puts it on the viewport frame
			local clone = part:Clone()
			clone.Parent = viewport
			
			--Deletes any clothing and decals
			for _, child in clone:GetDescendants() do
				if child:IsA("Shirt") or child:IsA("Pants") or child:IsA("Decal") or child:IsA("Texture") then
					child:Destroy()
				end
			end
			
			for _, child in clone:GetDescendants() do
				if child:IsA("MeshPart") then
					--Guves a red plastic material to the character parts and removes any texture
					child.Material = Enum.Material.SmoothPlastic
					child.Color = Color3.new(1, 0.231373, 0.0941176)
					if child.TextureID ~= nil then
						child.TextureID = ""
					end
					--Clones the current part and gives it a forcefield material before welding it to the original part
					--Give the x-ray a black outline
					local meshClone = child:Clone()
					meshClone.Parent = child
					meshClone.Material = Enum.Material.ForceField
					meshClone.Color = Color3.new(0, 0, 0)
					meshClone.CanCollide = false
					meshClone.Massless = true
					meshClone.Size *= 1.01
					local weld = Instance.new("WeldConstraint")
					weld.Parent = child
					weld.Part0 = child
					weld.Part1 = meshClone
					
				end
			end
			--Makes the clone character's Cframe follow the original character's Cframe
			game:GetService("RunService").RenderStepped:Connect(function()
				clone:PivotTo(part.PrimaryPart:GetPivot())
			end)
		end
	end
end

And here’s what it currently looks like in game:

A simple solution to this is just refreshing the viewport. Refreshing the viewport will also update the characters’ position if it changes. If don’t want to update the viewport and your characters don’t move, you could also try using GetPlayingAnimationTracks() then playing any currently playing tracks on the viewport clones.

After a lot of testing i manage to get this, which just copies the character’s body parts, sets the material, and then creates a run function that copies the Cframe of each body part to the cloned part:

for _, part in workspace:GetChildren() do
	if part:IsA("Model") then
		if part:FindFirstChildOfClass("Humanoid") then
			--THis clone the orginal character and puts it on the viewport frame
			local clone = part:Clone()
			clone.Parent = viewport
			
			--Deletes everything but the body parts
			for _, child in clone:GetDescendants() do
				if not child:IsA("MeshPart") then
					child:Destroy()
				end
			end
			
			for _, mesh in clone:GetChildren() do
				--gives body part a red material
				mesh.Material = Enum.Material.SmoothPlastic
				mesh.Color = Color3.new(1, 0.231373, 0.0941176)
				
				--Clones the current part and gives it a forcefield material
				--Give the x-ray a black outline
				local meshClone = mesh:Clone()
				meshClone.Parent = mesh
				meshClone.Material = Enum.Material.ForceField
				meshClone.Color = Color3.new(0, 0, 0)
				meshClone.CanCollide = false
				meshClone.Massless = true
				meshClone.Size *= 1.01
				meshClone.Parent = mesh
				
				--Makes the Cframes of the cloned parts equal the original's 
				game:GetService("RunService").RenderStepped:Connect(function()
					mesh.CFrame = part:FindFirstChild(mesh.Name).CFrame
					meshClone.CFrame = mesh.CFrame
				end)
			end
		end
	end
end

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