After testing and researching, I found a way to replicate the animations inside a ViewPortFrame.
Here is the complete process:
Step-by-Step Process
1. Clone the Character:
First, I created a function that clones only the visible and relevant parts of the character, excluding scripts and other unnecessary elements.
local player = game.Players.LocalPlayer
local function CloneCharacter(char)
local newModelCharacter = Instance.new("Model")
for i, part in pairs(char:GetChildren()) do
if not part:IsA("BaseScript") then
local newPart = part:Clone()
newPart.Parent = newModelCharacter
end
end
return newModelCharacter
end
2. Add the Clone to the WorldModel:
Once the character was cloned, I added it to the WorldModel inside the ViewPortFrame.
local PlayerCharacter = CloneCharacter(player.Character)
PlayerCharacter.Parent = WorldModel
3. Set up the Camera:
I created a camera for the ViewPortFrame and positioned it to properly show the character from a suitable distance.
local viewportCamera = Instance.new("Camera")
VPF.CurrentCamera = viewportCamera
viewportCamera.Parent = VPF
viewportCamera.CFrame = CFrame.new(PlayerCharacter.HumanoidRootPart.Position - Vector3.new(0, 0, 8))
* CFrame.Angles(0, math.rad(180), 0)
4. Update the Character Periodically:
I implemented a recursive function to continuously update the model inside the ViewPortFrame. This ensures that the character’s animations and changes are reflected.
local function UpdateCharacterView()
task.spawn(function()
local VPF: ViewportFrame = player.PlayerGui.Inventory.Content.EquipmentContent.CharacterView
local WM: WorldModel = VPF.WorldModel
WM:ClearAllChildren()
local PlayerCharacter = CloneCharacter(player.Character)
PlayerCharacter.Parent = WM
local viewportCamera = Instance.new("Camera")
VPF.CurrentCamera = viewportCamera
viewportCamera.Parent = VPF
viewportCamera.CFrame = CFrame.new(PlayerCharacter.HumanoidRootPart.Position - Vector3.new(0, 0, 5))
* CFrame.Angles(0, math.rad(180), 0)
task.wait()
UpdateCharacterView() -- Call itself again after waiting
end)
end
Complete Code
Here is the complete code I implemented:
local player = game.Players.LocalPlayer
local function CloneCharacter(char)
local newModelCharacter = Instance.new("Model")
for i, part in pairs(char:GetChildren()) do
if not part:IsA("BaseScript") then
local newPart = part:Clone()
newPart.Parent = newModelCharacter
end
end
return newModelCharacter
end
local function UpdateCharacterView()
task.spawn(function()
local VPF: ViewportFrame = player.PlayerGui.Inventory.Content.EquipmentContent.CharacterView
local WM: WorldModel = VPF.WorldModel
WM:ClearAllChildren()
local PlayerCharacter = CloneCharacter(player.Character)
PlayerCharacter.Parent = WM
local viewportCamera = Instance.new("Camera")
VPF.CurrentCamera = viewportCamera
viewportCamera.Parent = VPF
viewportCamera.CFrame = CFrame.new(PlayerCharacter.HumanoidRootPart.Position - Vector3.new(0, 0, 5))
* CFrame.Angles(0, math.rad(180), 0)
task.wait()
UpdateCharacterView()
end)
end
player.CharacterAdded:Connect(function()
UpdateCharacterView()
end)
Result
With this implementation, I was able to replicate the animations of the original character inside a ViewPortFrame.
Feedback
If anyone has a more efficient way to achieve this result, I would appreciate any suggestions or improvements.
I hope this is helpful to those who are trying to solve the same issue.