Ok so basically I am trying to make a script where the local player automatically looks at the nearest npc, but I have no idea how to do it. Can someone explain to me, and script it? Would appreciate it.
Player looking at npc help?
2 answers
Have you considered changing the CFrame or rotation of the player's HumanoidRootPart?
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local npcRoot = game.Workspace:WaitForChild("NPC"):WaitForChild("HumanoidRootPart") -- your npc's root part
wait(5) -- just waiting for the game to load
character:SetPrimaryPartCFrame(npcRoot.CFrame)
https://developer.roblox.com/en-us/api-reference/function/Model/SetPrimaryPartCFrame
Also you can use Magnitude to determine who is the closest NPC(if you haven't yet)
https://developer.roblox.com/en-us/articles/Magnitude
I haven't tested this out but I hope it helps a little bit or leads you in the right direction
1 comment
i'll def look into that, thanks
There are some ways, you see what fits you better:
Fist of all, a "get closer" function:
local Root = Character:WaitForChild("HumanoidRootPart")
local minDistance = 30
local nilPos = Vector3.new(0, 1000000000, 0)
local function getCloserNpcHead(TableOfNPCs)
-- faking a object's position to compare latter
local closerChar = {Head = {Position = nilPos }}
-- iterate through all characters and setting new closerChar
for i, v in pairs (TableOfNPCs) do
if (Root.Position - v.Head.Position).Magnitude < (Root.Position - closerChar.Head.Position).Magnitude then
if (Root.Position - v.Head.Position).Magnitude <= minDistance then
closerChar = v
end
end
end
-- Check if there is a new closerChar and return it
if closerChar.Head.Position ~= nilPos then
return
end
end
- Now, you have to manage to get a table with all the npc characters you want to calculate. Like, if they are all in a folder in workspace, you can call the function with
local CloserNPC = getCloserNpcHead(workspace.NPCsFolder)
You can also use Collection Service to better organize who is NPC and who is not.
-- Put a Script in the NPC with this to tag it:
local CollectionService = game:GetService("CollectionService")
CollectionService:AddTag("NPC", scriptParent)
then you can get all the character tagged with "NPC" to use our GetCloser function:
local CollectionService = game:GetService("CollectionService")
local npcTable = CollectionService:GetTagged("NPC")
local CloserNPC = getCloserNpcHead(npcTable)
Turning the head is a bit more complicated, it involves moving the Motor6D's CFrame. This are hard to make, but there are videos with examples on youtube like this: https://www.youtube.com/watch?v=mdXRnNb0S6o You will have to firgure out by yourself how to addapt to facing a part, unfortunatelly i cant do that.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
You could use CFrame.lookAt and a forcefield bubble.