Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a NPC look at the character?

I want to make a NPC move his body in the direction of the player's character when the player begins a dialog with the NPC. I saw this in a Roblox game called Monster Islands. Can somebody guide me?

1 answer

✓ Accepted answer

Ok so i've thought about it, and i just made it check if the player is close to the selected npc or not, and it turned out to be like this; This is in a Script that is located in the StarterCharacterScripts

First: Getting needed Variables

local char = script.Parent
local npcToFind = workspace:WaitForChild("StoreClerk")

After that we will need to calculate distance in a loop.

Second: Getting distance between player/NPC

spawn(function()
	while true do
		local TO = char.Torso
		local dist = (TO.Position - npcToFind.Torso.Position).magnitude
		wait()
	end
end

Now that we have the distance we can just do certain events if the player is close or not.

Third: Checking if close or not, and if player is close, make npc look towards the player, if not then continue the loop.

if dist <= 10 then
	npcToFind.Torso.CFrame = CFrame.new(npcToFind.Torso.Position, char.Torso.Position * Vector3.new(1,0,1) + char.Torso.Position * Vector3.new(0,1,0))
end

Full Code:

local char = script.Parent
local npcToFind = workspace.StoreClerk

spawn(function()
	while true do
		local TO = char.Torso
		local dist = (TO.Position - npcToFind.Torso.Position).magnitude
		if dist <= 10 then
			npcToFind.Torso.CFrame = CFrame.new(npcToFind.Torso.Position, char.Torso.Position*Vector3.new(1,0,1) + char.Torso.Position*Vector3.new(0, 1, 0))
		end
		wait()
	end
end)

Results: Gif Of the results Images: enter image description here enter image description here And by all of that, the NPC Will directly look at the player if the player is far away from the npc by 10 Studs you can always change that number, and if you want it to be a lot of npcs, you can just put the npcs in a list and constantly looping through that list.

Hope This Helped.

This discussion is preserved as it was. New answers, comments, and votes are closed.