Help with NPC rotating

  1. What do you want to achieve?
    have a NPC look at a player

  2. What is the issue?
    I can only rotate the head. If i rotate the body the legs just dangle in the air

  3. What solutions have you tried so far?
    I tried welding, looking on dev forum, and placing the rotating script in every body part.
    I found the script in the toolbox.
    script

local MaxLookDistance = 275
local MovementSpeed = 4

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Chat = game:GetService("Chat")
local Debris = game:GetService("Debris")
local Cheeky = script.Parent
local DefaultCFrame = Cheeky.CFrame

local function getNearestPlayer()
	local part = nil
	local dist = MaxLookDistance
	for i,v in ipairs(Players:GetPlayers()) do
		local character = v.Character
		if character then
			local root = character:FindFirstChild("Head")
			if root then
				local thisDist = (root.Position-Cheeky.Position).Magnitude
				if thisDist < dist then
					dist = thisDist
					part = root
				end
			end
		end
	end
	return part
end

local focusedPart = nil
local lastCheck = 0

RunService.Heartbeat:Connect(function(delta)
	if focusedPart then
		local cf = CFrame.lookAt(
			DefaultCFrame.Position,
			focusedPart.Position
		)
		Cheeky.CFrame = Cheeky.CFrame:Lerp(cf, delta * MovementSpeed)
	else
		Cheeky.CFrame = Cheeky.CFrame:Lerp(
			DefaultCFrame,
			delta * MovementSpeed
		)
	end
	if time() > lastCheck then
		lastCheck = time() + 1
		focusedPart = getNearestPlayer()
		
	end
end)

Cheeky.Anchored = true

If the NPC has a HumanoidRootPart as it’s PrimaryPart and you want the NPC to face something you can do:

local NPCroot = NPC.HumanoidRootPart

NPCroot.CFrame = CFrame.new(NPCroot.Position, target.Position)

If you want it to rotate a certain amount of degrees you can do:

local NPCroot = NPC.HumanoidRootPart

NPCroot.CFrame = NPCroot.CFrame * CFrame.Angles(math.rad(xDegree), math.rad(yDegree), math.rad(zDegree))

new script

local MaxLookDistance = 275
local MovementSpeed = 4

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Chat = game:GetService("Chat")
local Debris = game:GetService("Debris")
local NPCroot = script.Parent.Parent.HumanoidRootPart


local DefaultCFrame = NPCroot.CFrame

local function getNearestPlayer()
	local part = nil
	local dist = MaxLookDistance
	for i,v in ipairs(Players:GetPlayers()) do
		local character = v.Character
		if character then
			local root = character:FindFirstChild("Head")
			if root then
				local thisDist = (root.Position-NPCroot.Position).Magnitude
				if thisDist < dist then
					dist = thisDist
					part = root
				end
			end
		end
	end
	return part
end

local focusedPart = nil
local lastCheck = 0

RunService.Heartbeat:Connect(function(delta)
	if focusedPart then
		local cf = CFrame.lookAt(
			DefaultCFrame.Position,
			focusedPart.Position
		)
		NPCroot.CFrame = NPCroot.CFrame:Lerp(cf, delta * MovementSpeed)
	else
		NPCroot.CFrame = NPCroot.CFrame:Lerp(
			DefaultCFrame,
			delta * MovementSpeed
		)
	end
	if time() > lastCheck then
		lastCheck = time() + 1
		focusedPart = getNearestPlayer()
		
	end
end)

NPCroot.Anchored = true

still does not work.

Can you use prints to locate the error please.

no errors at all. all prints work.

What is happening with the NPC? Is it rotating only one part? Not rotating at all?

Not rotating at all. :frowning: :frowning: :frowning: :frowning: :-1:

Have you made sure the NPC model has the PrimaryPart set to it’s own HumanoidRootPart?

Yep. :frowning: :frowning: :frowning: :frowning: ( "( :frowning: :frowning: :frowning: :frowning:

I can’t really pin point where the error is but this code makes the NPC smoothly look at a target.

local RS = game:GetService("RunService")

workspace.Part.ClickDetector.MouseClick:Connect(function(player)
	local root = workspace.Dummy.HumanoidRootPart
	local target = workspace.Part
	local movementSpeed = 4
	RS.Heartbeat:Connect(function(delta)
		local cf = CFrame.lookAt(root.Position, target.Position)
		root.CFrame = root.CFrame:Lerp(cf,delta  * movementSpeed)
	end)
end)

You can try to use this and modify it to fit your script and see what happens then.
If you don’t want the NPC to look up with it’s whole model you can just modify it to where it only looks with certain axis’s.

Head over to the Plugins tab when you open studio and click Build Rig. Select whichever rig you want. Add an script inside the model. Enter this code :


local username = your username

game:GetService("RunService").Heartbeat:Connect(function()

script.Parent.PrimaryPart.CFrame = CFrame.lookAt(script.Parent.PrimaryPart.Position, workspace:WaitForChild(username).HumanoidRootPart.Position)

end)

But i want it look at anybody that comes close.
current script

local MaxLookDistance = 100
local MovementSpeed = 5

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Chat = game:GetService("Chat")
local Debris = game:GetService("Debris")
local Cheeky = script.Parent
local DefaultCFrame = Cheeky.CFrame

local function getNearestPlayer()
	local part = nil
	local dist = MaxLookDistance
	for i,v in ipairs(Players:GetPlayers()) do
		local character = v.Character
		if character then
			local root = character:FindFirstChild("Head")
			if root then
				local thisDist = (root.Position-Cheeky.Position).Magnitude
				if thisDist < dist then
					dist = thisDist
					part = root
				end
			end
		end
	end
	return part
end

local focusedPart = nil
local lastCheck = 0

RunService.Heartbeat:Connect(function(delta)
	if focusedPart then
		local cf = CFrame.lookAt(
			DefaultCFrame.Position,
			focusedPart.Position
		)
		Cheeky.CFrame = Cheeky.CFrame:Lerp(cf, delta * MovementSpeed)
	else
		Cheeky.CFrame = Cheeky.CFrame:Lerp(
			DefaultCFrame,
			delta * MovementSpeed
		)
	end
	if time() > lastCheck then
		lastCheck = time() + 1
		focusedPart = getNearestPlayer()
	end
end)

Cheeky.Anchored = true

Try this :

local closest_player = nil
local magnitude = nil

game:GetService("RunService").Heartbeat:Connect(function()
	for i,v in pairs(game.Players:GetChildren()) do
		if v.Character and (v.Character.HumanoidRootPart.Position - script.Parent.PrimaryPart.Position).Magnitude < 100 then
			if closest_player == nil or v == closest_player then
				closest_player = v
				magnitude = (v.Character.HumanoidRootPart.Position - script.Parent.PrimaryPart.Position).Magnitude
				script.Parent.PrimaryPart.CFrame = CFrame.lookAt(script.Parent.PrimaryPart.Position, v.Character.HumanoidRootPart.Position)
			elseif (v.Character.HumanoidRootPart.Position - script.Parent.PrimaryPart.Position).Magnitude < magnitude and v ~= closest_player then
				closest_player = v
				magnitude = (v.Character.HumanoidRootPart.Position - script.Parent.PrimaryPart.Position).Magnitude
				script.Parent.PrimaryPart.CFrame = CFrame.lookAt(script.Parent.PrimaryPart.Position, v.Character.HumanoidRootPart.Position)
			end
		end
	end
	if closest_player and closest_player.Character and (closest_player.Character.HumanoidRootPart.Position - script.Parent.PrimaryPart.Position).Magnitude > 100 then
		closest_player = nil
		magnitude = nil
	end
end)

What this script does : Whenever there is a player in range, the npc will look at them until they are not in range or another player is closer.

Also if you don’t want the npc to rotate awkwardly, you should constantly update the orientation of the npc like this :

script.Parent.PrimaryPart.Orientation -= Vector3.new(script.Parent.PrimaryPart.Orientation.X,0,script.Parent.PrimaryPart.Orientation.Z) 

Try using the lookAt Method. Essentially what you would do is give the At and Look position in the parameters of this method, a quick example:

local Model = workspace.Model 

local LookAt = workspace.LookAtThisModel

-- If both models have a Primary Part 

Model.PrimaryPart.CFrame = CFrame.lookAt(Model.PrimaryPart.Position,LookAt.PrimaryPart.Position)

You can find more info here under the CFrame.lookAt box: lookAt

Hope this helped :slight_smile:

If i rotate the body the legs just dangle in the air

Does not work. :frowning: :frowning: :frowning: :frowning: :frowning: :frowning:

you should have a video to help us visualize

For what the script in every pice or the person?

Maybe if I made the whole NPC a union…