Character position is wrong on viewport frame

Hello, I tried a profile picture UI in a project. But it didn’t work, something went wrong. So there is in game avatars for each role and each rank. For example Scientist role and Novice Researcher rank. When player select their characters the profile picture changes with a local script. Also all these in game avatars is in ReplicatedStorage.

The problem is when the local script clones the in game avatar and send it to viewport frame and deletes old avatar. The new avatars position is wrong.

I have look some tutorials on youtube and I looked some topics on devforum but I didn’t find what I want.

Here is the Local Script:

local player = game:GetService("Players").LocalPlayer
local viewportframe = script.Parent:WaitForChild("ViewportFrame")
local Avatars = game:GetService("ReplicatedStorage"):WaitForChild("Avatars")

local rigCFrame = viewportframe.Rig.HumanoidRootPart.CFrame

local role = player:WaitForChild("Role")
local access = player:WaitForChild("Access")

access.Changed:Connect(function(newAccess)
	viewportframe.Rig:Destroy()
	
	local newAvatar = Avatars:FindFirstChild(player.Role.Value):FindFirstChild(newAccess):Clone()
	newAvatar.PrimaryPart = newAvatar:FindFirstChild("HumanoidRootPart")
	newAvatar.HumanoidRootPart.CFrame = rigCFrame
	newAvatar.Parent = viewportframe
end)
1 Like