ViewportFrame Accsessory issue

hi guys i have a problem about the viewport frame there is a thing i cant see the avatars items like hats, beards etc

local function DisplayCharacterInViewport(viewportFrame, character, viewportId)
	if not viewportFrame then
		repeat task.wait() until viewportFrame ~= nil
	end

	if not character or not character.Parent then
		character = Players.LocalPlayer.Character or Players.LocalPlayer.CharacterAdded:Wait()
	end

	viewportFrame:ClearAllChildren()

	if rotationConnections[viewportId] then
		rotationConnections[viewportId]:Disconnect()
		rotationConnections[viewportId] = nil
	end

	local cam = Instance.new("Camera")
	cam.Parent = viewportFrame
	viewportFrame.CurrentCamera = cam

	local modelClone = Instance.new("Model")
	modelClone.Name = "CharacterClone"
	modelClone.Parent = viewportFrame

	
	for _, obj in ipairs(character:GetChildren()) do
		if obj:IsA("BasePart") or obj:IsA("MeshPart") or obj:IsA("Accessory")
			or obj:IsA("Shirt") or obj:IsA("Pants") or obj:IsA("ShirtGraphic")
			or obj:IsA("BodyColors") or obj:IsA("HumanoidDescription")
			or obj:IsA("Humanoid") then
			local cloneObj = obj:Clone()
			cloneObj.Parent = modelClone
		end
	end

	
	for _, cloneAccessory in ipairs(modelClone:GetChildren()) do
		if cloneAccessory:IsA("Accessory") then
			local handle = cloneAccessory:FindFirstChild("Handle")
			if handle then
				
				for _, weld in ipairs(handle:GetChildren()) do
					if weld:IsA("Weld") or weld:IsA("WeldConstraint") then
						weld:Destroy()
					end
				end

				
				local accessoryAttachment = handle:FindFirstChildOfClass("Attachment")
				if accessoryAttachment then
					-
					local head = modelClone:FindFirstChild("Head")
					if head then
						local headAttachment = head:FindFirstChild(accessoryAttachment.Name)
						if headAttachment then
							
							local newWeld = Instance.new("Weld")
							newWeld.Part0 = head
							newWeld.Part1 = handle
							newWeld.C0 = headAttachment.CFrame
							newWeld.C1 = accessoryAttachment.CFrame
							newWeld.Parent = handle
						end
					end
				else
					
					local head = modelClone:FindFirstChild("Head")
					if head then
						local newWeld = Instance.new("Weld")
						newWeld.Part0 = head
						newWeld.Part1 = handle
						newWeld.C0 = CFrame.new(0, 0.5, 0)
						newWeld.C1 = CFrame.new(0, 0, 0)
						newWeld.Parent = handle
					end
				end

				
				handle.CanCollide = false
			end
		end
	end

	
	for _, p in ipairs(modelClone:GetDescendants()) do
		if p:IsA("BasePart") then
			p.Anchored = true
		end
	end

	local root = modelClone:FindFirstChild("HumanoidRootPart") or modelClone:FindFirstChild("Head")
	if not root then return end

	modelClone.PrimaryPart = root

	local light = Instance.new("PointLight")
	light.Brightness = 2
	light.Range = 15
	light.Parent = cam

	local rotationY = 180
	local rotationX = 10
	local zoomDistance = 6
	local minZoom, maxZoom = 3, 10
	local rotating = false
	local lastMousePos = Vector2.new()
	local sensitivity = 0.3

	modelClone:SetPrimaryPartCFrame(CFrame.new(0, -2, 0) * CFrame.Angles(0, math.rad(rotationY), 0))

	viewportFrame.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			rotating = true
			lastMousePos = input.Position
		end
	end)
	viewportFrame.InputEnded:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			rotating = false
		end
	end)
	viewportFrame.InputChanged:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseWheel then
			zoomDistance = math.clamp(zoomDistance - input.Position.Z * 0.5, minZoom, maxZoom)
		elseif input.UserInputType == Enum.UserInputType.MouseMovement and rotating then
			local delta = input.Position - lastMousePos
			lastMousePos = input.Position
			rotationY -= delta.X * sensitivity
			rotationX = math.clamp(rotationX - delta.Y * sensitivity, -25, 25)
		end
	end)

	rotationConnections[viewportId] = RunService.RenderStepped:Connect(function()
		if not (modelClone and modelClone.Parent and character and character.Parent) then 
			if rotationConnections[viewportId] then
				rotationConnections[viewportId]:Disconnect()
				rotationConnections[viewportId] = nil
			end
			return 
		end

	
		for _, originalPart in ipairs(character:GetDescendants()) do
			if originalPart:IsA("BasePart") then
				local clonePart = modelClone:FindFirstChild(originalPart.Name)
				if clonePart and clonePart:IsA("BasePart") then
					clonePart.CFrame = originalPart.CFrame
				end
			end
		end

		local radiansY = math.rad(rotationY)
		local radiansX = math.rad(rotationX)
		local offset = CFrame.Angles(0, radiansY, 0) * CFrame.Angles(radiansX, 0, 0)
		local camPos = (root.CFrame * offset).Position + offset.LookVector * -zoomDistance

		cam.CFrame = CFrame.new(camPos, root.Position + Vector3.new(0, 1.5, 0))
	end)
end
1 Like

Are you using a worldmodel, physics don’t work in a viewport unless they are within a worldmodel, this includes welds.

1 Like

Have you tried using :GetDescendants? Accessories usually have a BasePart with a special mesh in it which shows the actual Accessory

So I would change This line

for _, obj in ipairs(character:GetChildren()) do
	if obj:IsA("BasePart") or obj:IsA("MeshPart") or obj:IsA("Accessory")
		or obj:IsA("Shirt") or obj:IsA("Pants") or obj:IsA("ShirtGraphic")
		or obj:IsA("BodyColors") or obj:IsA("HumanoidDescription")
		or obj:IsA("Humanoid") then
		local cloneObj = obj:Clone()
		cloneObj.Parent = modelClone
	end
end

To this

for _, obj in ipairs(character:GetDescendants()) do
	if obj:IsA("BasePart") or obj:IsA("MeshPart") or obj:IsA("Accessory")
			or obj:IsA("Shirt") or obj:IsA("Pants") or obj:IsA("ShirtGraphic")
			or obj:IsA("BodyColors") or obj:IsA("HumanoidDescription")
			or obj:IsA("Humanoid") then
		local cloneObj = obj:Clone()
		cloneObj.Parent = modelClone
	end
end
2 Likes

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