Viewportframe scripting help [ I FIXED IT]

i have a issue about the accsesorys like their position are random and some accssesorys not exist on character
– issued one


-normal avatar

local function DisplayCharacterInViewport(viewportFrame, character, viewportId)
	if not character then return 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


	local accessoryConnections = {}
	
	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
			
			
			if obj:IsA("Accessory") then
				local handle = cloneObj:FindFirstChild("Handle")
				if handle then
					
					local originalWeld = obj:FindFirstChild("Handle") and obj.Handle:FindFirstChildOfClass("Weld")
					if originalWeld then
						local part0Name = originalWeld.Part0.Name
						local part1Name = originalWeld.Part1.Name
						
						
						local clonePart0 = modelClone:FindFirstChild(part0Name)
						local clonePart1 = handle
						
						if clonePart0 and clonePart1 then
							
							for _, w in ipairs(handle:GetDescendants()) do
								if w:IsA("Weld") then
									w:Destroy()
								end
							end
							
						
						end
					end
				end
			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


	modelClone:SetPrimaryPartCFrame(root.CFrame * CFrame.Angles(0, math.rad(0), 0))

	
	local rotationY = 0
	local rotationX = 15
	local zoomDistance = 4
	local minZoom, maxZoom = 2.5, 7


	local rotating = false
	local lastMousePos = Vector2.new()
	local sensitivity = 0.3

	
	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 = rotationY - delta.X * sensitivity
			rotationX = math.clamp(rotationX - delta.Y * sensitivity, -25, 25)
		end
	end)

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

	
	rotationConnections[viewportId] = RunService.RenderStepped:Connect(function()
		if modelClone and modelClone.PrimaryPart then
		
			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 = (modelClone.PrimaryPart.CFrame * offset).Position + offset.LookVector * -zoomDistance

			cam.CFrame = CFrame.new(camPos, modelClone.PrimaryPart.Position + Vector3.new(0, 1.5, 0))
		else
			if rotationConnections[viewportId] then
				rotationConnections[viewportId]:Disconnect()
				rotationConnections[viewportId] = nil
			end
		end
	end)
end

im try to fix tihs for 1 hour im gonna crash the pc soon

Accessories use Attachments of the same name parented to their Handle and a limb (such as the Head) in order to position themselves, which you are not cloning.

If you insert an Attachment into the Accessory’s Handle with the same name as an Attachment in one of the character’s limbs, they connect and ignore properties inherited from the Accoutrement class. Otherwise, the Accessory functions identically to a Hat.

Add a or obj:IsA("Attachment") check to your for loop:

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") or obj:IsA("Attachment") then -- here

make sure the character parent is attached to a WorldModel inside the ViewportFrame

i fixed it no worries &^+‘+!’+%^+&^+&4326

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