Help with viewmodel

I dont even know what to say here, ive many a few other posts on this which have only led me into more problems

Cant figure out how to setup this viewmodel so it can be animated, the limbs just all teleport into the root part after hooking up the motors, not sure what to even do to try and combat this.

Heres the code

(edit, Updated)

local Player = game:GetService("Players").LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()
Character.Archivable = true
repeat task.wait() until game:IsLoaded()
local sf = string.find

local viewmodel = Instance.new("Model")
viewmodel.Name = "ViewModel"

local ClonedChar = Character:Clone()

local RootPart

for Num, Part in ipairs(ClonedChar:GetChildren()) do
	local name = Part.Name:lower()
	
	if not sf(name, "hand") and not sf(name, "arm") and not sf(name, "head") then
		if not Part:IsA("Humanoid") and not Part:IsA("Shirt") and not string.find(Part.Name:lower(), "root") then
			Part:Destroy()
		elseif Part:IsA("Humanoid") then
			Part.Parent = viewmodel
			for i, v in ipairs(Part:GetChildren()) do
				if not v:IsA("Animator") then v:Destroy()end
			end
		elseif string.find(Part.Name:lower(), "root") then
			RootPart = Part
			Part.Parent = nil
			Part.Size = Vector3.new(.75,.75,.75)
			Part.CanCollide = false
			Part.CanQuery = false
			Part.CanTouch = false
			Part.Transparency = 1
			Part.Anchored = true
		else
			Part.Parent = viewmodel
		end
	else
		if not Part:IsA("MeshPart") then
			Part:Destroy()
		else
			Part.Anchored = true
			Part.Parent = viewmodel
			for i, v in ipairs(Part:GetChildren()) do
				if not v:IsA("Motor6D") then
					v:Destroy()
				end
			end
		end
	end
end
ClonedChar:Destroy()
viewmodel.Parent = workspace.Camera
local Humanoid = viewmodel:WaitForChild("Humanoid")

local CameraPart = Instance.new("Part"); CameraPart.Name = "CameraPart"
CameraPart.Anchored = true
CameraPart.CanCollide = false
CameraPart.Size = Vector3.new(.75,.75,.75)
CameraPart.Parent = viewmodel
CameraPart.CFrame = viewmodel:WaitForChild("Head").CFrame
viewmodel:WaitForChild("Head"):Destroy()

Humanoid.Parent = nil
RootPart.Parent = viewmodel
RootPart.CFrame = CameraPart.CFrame

local LeftHand = viewmodel:WaitForChild("LeftHand")
local LeftLowerArm = viewmodel:WaitForChild("LeftLowerArm")
local LeftUpperArm = viewmodel:WaitForChild("LeftUpperArm")

local RightHand = viewmodel:WaitForChild("RightHand")
local RightLowerArm = viewmodel:WaitForChild("RightLowerArm")
local RightUpperArm = viewmodel:WaitForChild("RightUpperArm")

local Rpp = RootPart.Position
local LA = LeftUpperArm.Position
local RA = RightUpperArm.Position

local LArmOffset = Vector3.new(
	Rpp.X - LA.x,
	-(Rpp.Y - LA.Y),
	Rpp.Z - LA.Z
)

local RArmOffset = Vector3.new(
	Rpp.X - RA.x,
	-(Rpp.Y - RA.Y),
	Rpp.Z - RA.Z
)

print(RArmOffset)

local Rshoulder = RightUpperArm:FindFirstChild("RightShoulder")
local Lshoulder = LeftUpperArm:FindFirstChild("LeftShoulder")
Rshoulder.Part0 = RootPart
Lshoulder.Part0 = RootPart
Rshoulder.C0 = CFrame.new(RArmOffset)
Lshoulder.C0 = CFrame.new(LArmOffset)
Rshoulder.C1 = CFrame.new(0,0,0)
Lshoulder.C1 = CFrame.new(0,0,0)

local Motor3 = Instance.new("Motor6D")
Motor3.Parent = RootPart
Motor3.Part0 = RootPart
Motor3.Part1 = CameraPart
Humanoid.Parent = viewmodel

for i, v in ipairs(viewmodel:GetChildren()) do
	local Name = v.Name:lower()
	if not string.find(Name, "root") and v:IsA("MeshPart") then
		v.Anchored = false
	end
end
1 Like

Not sure why you’re deleting the Motor6Ds then making new ones, but Motor6Ds need more done with then just setting the Part properties. You would have to set the C0 and C1 properties alongside them so they actually have an idea on where they’re supposed to be.

1 Like

Not entirely sure where im deleting motor6ds at

Hm ok, is there a method to get the amount i should put into c0 and c1?

My mistake, I see the not Motor6D now

I cannot fathom the math behind it currently, but I would look at the Motor6Ds through the properties window to get a general idea of how they work.

1 Like

Kinda just winged it and it worked,
first thing that came to mind was this

local Rpp = RootPart.Position
local LA = LeftUpperArm.Position
local RA = RightUpperArm.Position

local LArmOffset = Vector3.new(
	Rpp.X - LA.x,
	Rpp.Y - LA.Y,
	Rpp.Z - LA.Z
)

local RArmOffset = Vector3.new(
	Rpp.X - RA.x,
	Rpp.Y - RA.Y,
	Rpp.Z - RA.Z
)

and it worked, although now after unanchoring the the limbs they are stuck with cancollide on

also updated the code in the main post