How to make a npc look at the nearest player

  1. What do you want to achieve?
    I want to make the npc look at the nearest player and walk to the player, without any animations.

  2. What is the issue?

MonsterLooking2
MonsterLooking

  1. What solutions have you tried so far?
    I’ve tried searching all roblox tutorials on youtube but nothing really worked.

task.wait(3)


local Monster = script.Parent

local PlayerService = game:GetService("Players")
local Distance = 30 -- 

function UpdateMonsterPos()
	while true do

		for _, player in ipairs(PlayerService:GetPlayers()) do
			
			local PlayerCharacter = player.Character
			
			if PlayerCharacter then
				local PlayerPosition = PlayerCharacter.PrimaryPart.Position
				local MonsterPos = Monster.PrimaryPart.Position
				local MonsterPrimaryPart = Monster.PrimaryPart
				
				if (PlayerPosition - MonsterPos).Magnitude <= Distance then
					local Direction = (PlayerPosition - MonsterPos).Unit
					
					Monster:SetPrimaryPartCFrame(CFrame.new(MonsterPos + Direction))
					
					local lookAtRotation = CFrame.new(MonsterPos, PlayerPosition)
					Monster:SetPrimaryPartCFrame(lookAtRotation)
				end
			end
		end
		task.wait(0.5)
	end
end

UpdateMonsterPos()

He looks at the player but does not walk to them.

2 Likes
task.wait(3)


local Monster = script.Parent

local PlayerService = game:GetService("Players")
local Distance = 30 -- 

function UpdateMonsterPos()
	while true do

		for _, player in ipairs(PlayerService:GetPlayers()) do
			
			local PlayerCharacter = player.Character
			
			if PlayerCharacter then
				local PlayerPosition = PlayerCharacter.PrimaryPart.Position
				local MonsterPos = Monster.PrimaryPart.Position
				local MonsterPrimaryPart = Monster.PrimaryPart
				
				if (PlayerPosition - MonsterPos).Magnitude <= Distance then
					local Direction = (PlayerPosition - MonsterPos).Unit
					
					Monster:SetPrimaryPartCFrame(CFrame.new(MonsterPos + Direction)*CFrame.new(1,0,1))
					
					local lookAtRotation = CFrame.new(MonsterPos, PlayerPosition)
					Monster:SetPrimaryPartCFrame(lookAtRotation)
				end
			end
		end
		task.wait(0.5)
	end
end

UpdateMonsterPos()

Multiplying a CFrame by CFrame.new(1,0,1) makes it so it doesn’t tilt up or down

3 Likes

Thanks, but the monster still doesn’t run at the player. Can you please help?

Uhhhhhhhhhhhhhhhhhhh I forgot how to do this and I’m on mobile, pretty sure you need to use humanoid:MoveTo() though

1 Like

ooooooooooooooooooooo I’ll try.

1 Like

Uhmmmmmm

You can get the direction of the player and add in on to the monsters position while multiplying by a speed multiplier in this part of the code:

local lookAtRotation = CFrame.new(MonsterPos, PlayerPosition)
Monster:SetPrimaryPartCFrame(lookAtRotation)

Here is how I’d go about it:

local dir = (PlayerPosition - MonsterPos).Unit -- Direction vector
local lookAtRotation = CFrame.new(MonsterPos + (dir * 1) ,PlayerPosition) -- instead of one you can do .1 etc etc, however you need to make sure the script updates fast enough for the movement
Monster:SetPrimaryPartCFrame(lookAtRotation)
1 Like

Thanks man, it works!!!

1 Like

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