I want to show player characters and npcs in menu like this
It works in studio but in game sometimes it shows player character and sometimes it doesn’t while npcs are showing properly both in game and in studio
I checked character’s and camera’s cframe and they are the same both in game and in studio and checked whether character is properly parented.
This is the code i use both for player characters and npcs. There also aren’t any errors in output.
local function getCharacter(charName: string)
local character
if charName == "player" then
player.Character.Archivable = true
character = player.Character:Clone()
player.Character.Archivable = false
character.Name = "player"
for i,v in ipairs(character:GetChildren()) do
if v:IsA("Script") or v:IsA("LocalScript") or v:IsA("Folder") then
v:Destroy()
end
end
else
character = game.ReplicatedStorage.NPC:FindFirstChild(charName):Clone()
end
character:SetPrimaryPartCFrame(CFrame.new(0, 0, 0))
return character
end
function module.new(charName: string, vFrame, section: string | Vector3, angle: string?)
local charViewport = {
character = nil,
section = section or "WholeCharacter",
angle = typeof(section) == "string" and (angle or "Center") or section.X,
camera = nil,
vFrame = vFrame,
}
setmetatable(charViewport, module)
charViewport.charName = charName
charViewport.character = getCharacter(charName)
-- Stop all animations
if charName == "player" then
-- Character needs to be parented to world model so that animations can be stopped
charViewport.character.Parent = workspace
for i,v in ipairs(charViewport.character.Humanoid:GetPlayingAnimationTracks()) do
v:Stop()
end
charViewport.character.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
-- Wait for animations to stop in workspace
task.wait()
end
-- Parent character
if charViewport.vFrame:FindFirstChild("WorldModel") then
charViewport.character.Parent = charViewport.vFrame.WorldModel
else
charViewport.character.Parent = charViewport.vFrame
end
-- Setup camera
charViewport.camera = Instance.new("Camera")
charViewport.camera.CameraType = Enum.CameraType.Scriptable
charViewport.camera.CFrame = calculateCameraCFrame(charViewport)
charViewport.camera.Parent = charViewport.vFrame
charViewport.vFrame.CurrentCamera = charViewport.camera
return charViewport
end