What do you want to achieve?
have a NPC look at a player
What is the issue?
I can only rotate the head. If i rotate the body the legs just dangle in the air
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
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
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
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 :
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