Parts not aligning when copying players movement


I’m trying to make an npc which copies all your movements, however the hair accessory isn’t aligning to where it should be, and when I tried adjusting it with Vector3.new it just wasn’t consistent and would sway from side to side (the vector3.new edit isn’t showcased in the video above)

local RNS = game:GetService("RunService")
local plr = game.Players.LocalPlayer
plr.CharacterAdded:Wait()
local char = plr.Character

local npc = game.Workspace:WaitForChild("NoahFollow")
for i,part in npc:GetChildren() do
	if part:IsA("MeshPart") then
		if part.Name ~= "Head" then
			part.Anchored = true
			part.CanCollide = true
		end
	end
end
RNS.RenderStepped:Connect(function(delta)
	local head =char:FindFirstChild("Head").Position
	--local hair=head+Vector3.new(0,0.25,-0.25)
	local HRP=char:FindFirstChild("HumanoidRootPart").Position
	local LUA= char:FindFirstChild("LeftUpperArm").Position
	local LLA = char:FindFirstChild("LeftLowerArm").Position
	local LH=char:FindFirstChild("LeftHand").Position
	local LUL=char:FindFirstChild("LeftUpperLeg").Position
	local LLL=char:FindFirstChild("LeftLowerLeg").Position
	local LF= char:FindFirstChild("LeftFoot").Position
	local RUA= char:FindFirstChild("RightUpperArm").Position
	local RLA=char:FindFirstChild("RightLowerArm").Position
	local RH=char:FindFirstChild("RightHand").Position
	local RUL=char:FindFirstChild("RightUpperLeg").Position
	local RLL= char:FindFirstChild("RightLowerLeg").Position
	local RF=char:FindFirstChild("RightFoot").Position
	local LT=char:FindFirstChild("LowerTorso").Position
	local UT=char:FindFirstChild("UpperTorso").Position
	

	local HO=char:FindFirstChild("Head").Orientation
	local HRPO= char:FindFirstChild("HumanoidRootPart").Orientation
	local LUAO= char:FindFirstChild("LeftUpperArm").Orientation
	local LLAO=char:FindFirstChild("LeftLowerArm").Orientation
	local LHO=char:FindFirstChild("LeftHand").Orientation
	local LULO=char:FindFirstChild("LeftUpperLeg").Orientation
	local LLLO=char:FindFirstChild("LeftLowerLeg").Orientation
	local LFO=char:FindFirstChild("LeftFoot").Orientation
	local RUAO= char:FindFirstChild("RightUpperArm").Orientation
	local RLAO=char:FindFirstChild("RightLowerArm").Orientation
	local RHO=char:FindFirstChild("RightHand").Orientation
	local RULO=char:FindFirstChild("RightUpperLeg").Orientation
	local RLLO=char:FindFirstChild("RightLowerLeg").Orientation
	local RFO=char:FindFirstChild("RightFoot").Orientation
	local LTO= char:FindFirstChild("LowerTorso").Orientation
	local UTO= char:FindFirstChild("UpperTorso").Orientation

	wait(1.5)
	npc:FindFirstChild("Head").Position=head
	npc:FindFirstChild("Pal Hair").Handle.Position=head
	npc:FindFirstChild("HumanoidRootPart").Position = HRP
	npc:FindFirstChild("LeftUpperArm").Position =LUA
	npc:FindFirstChild("LeftLowerArm").Position =LLA
	npc:FindFirstChild("LeftHand").Position = LH
	npc:FindFirstChild("LeftUpperLeg").Position = LUL
	npc:FindFirstChild("LeftLowerLeg").Position =LLL
	npc:FindFirstChild("LeftFoot").Position =LF
	npc:FindFirstChild("RightUpperArm").Position =RUA
	npc:FindFirstChild("RightLowerArm").Position = RLA
	npc:FindFirstChild("RightHand").Position = RH
	npc:FindFirstChild("RightUpperLeg").Position =RUL
	npc:FindFirstChild("RightLowerLeg").Position =RLL
	npc:FindFirstChild("RightFoot").Position =RF
	npc:FindFirstChild("LowerTorso").Position =LT
	npc:FindFirstChild("UpperTorso").Position =UT
	
	npc:FindFirstChild("Head").Orientation =HO
	npc:FindFirstChild("HumanoidRootPart").Orientation = HRPO
	npc:FindFirstChild("LeftUpperArm").Orientation = LUAO
	npc:FindFirstChild("LeftLowerArm").Orientation = LLAO
	npc:FindFirstChild("LeftHand").Orientation = LHO
	npc:FindFirstChild("LeftUpperLeg").Orientation = LULO
	npc:FindFirstChild("LeftLowerLeg").Orientation =LLLO
	npc:FindFirstChild("LeftFoot").Orientation =LFO
	npc:FindFirstChild("RightUpperArm").Orientation =RUAO
	npc:FindFirstChild("RightLowerArm").Orientation =RLAO
	npc:FindFirstChild("RightHand").Orientation =RHO
	npc:FindFirstChild("RightUpperLeg").Orientation = RULO
	npc:FindFirstChild("RightLowerLeg").Orientation =RLLO
	npc:FindFirstChild("RightFoot").Orientation =RFO
	npc:FindFirstChild("LowerTorso").Orientation = LTO
	npc:FindFirstChild("UpperTorso").Orientation = UTO
	npc:FindFirstChild("Pal Hair").Handle.Orientation=HO
	
	local distance =(char:FindFirstChild("HumanoidRootPart").Position-npc:FindFirstChild("HumanoidRootPart").Position).magnitude

	if distance < 2 then --makes the npc invisible when near player
		local transparency = 1- (distance/2)
		npc.Head.face.Transparency=transparency
		for i,part in npc:GetChildren() do
			if part:IsA("MeshPart") then
				part.Transparency = transparency
			elseif part:IsA("Accessory") then
				part.Handle.Transparency = transparency
			end
		end
	else
		npc.Head.face.Transparency=0
		for i,part in npc:GetChildren() do
			if part:IsA("MeshPart") then
				part.Transparency = 0
			elseif part:IsA("Accessory") then
				part.Handle.Transparency = 0
			end
		end
	end
end)

Instead of searching for each part manually, make a loop and set the CFrames:

local RNS = game:GetService("RunService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local npc = workspace:WaitForChild("NoahFollow")

for _, part in npc:GetChildren() do
	if part:IsA("MeshPart") and part.Name ~= "Head" then
		part.Anchored = true
		part.CanCollide = true
	end
end

local bodyPartNames ={
	"Head", "HumanoidRootPart", "LeftUpperArm", "LeftLowerArm", "LeftHand", 
	"LeftUpperLeg", "LeftLowerLeg", "LeftFoot", "RightUpperArm", "RightLowerArm", 
	"RightHand", "RightUpperLeg", "RightLowerLeg", "RightFoot", "LowerTorso", "UpperTorso"
}

RNS.RenderStepped:Connect(function(delta)
	-- build cframe dictionary
	local cframes = {}
	
	for _, partName in bodyPartNames do
		local part = char[partName]
		cframes[partName] = part.CFrame
	end
	
	-- apply cframes after wait
	task.wait(1.5)
	
	for _, partName in bodyPartNames do
		local part = npc[partName]
		part.CFrame = cframes[partName]
	end
	
	-- custom offset for hair
	npc["Pal Hair"].CFrame = cframes.Head * CFrame.new(0, 0.25, -0.25)

	local distance =(char.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).magnitude

	if distance < 2 then --makes the npc invisible when near player
		local transparency = 1 - (distance/2)
		npc.Head.face.Transparency = transparency
		for _, part in npc:GetChildren() do
			if part:IsA("MeshPart") then
				part.Transparency = transparency
			elseif part:IsA("Accessory") then
				part.Handle.Transparency = transparency
			end
		end
	else
		npc.Head.face.Transparency = 0
		for _, part in npc:GetChildren() do
			if part:IsA("MeshPart") then
				part.Transparency = 0
			elseif part:IsA("Accessory") then
				part.Handle.Transparency = 0
			end
		end
	end
end)

You should also apply the hair offset via cframe so the direction is relative/local instead of global.

1 Like

This works! I appreciate the help, although for some reason the hair didn’t need to be offset

1 Like

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