How to make a Smart NPC?

Hello developers,

I am currently working on a Horror Game and there is a “monster” in it. I just want this NPC to get Smarter, for example, avoiding things by itself and jumping. I will post the script below and I hope someone can help me further because I have no idea how to do this.

The Script:

function findNearestPlayer(Position)
wait(0.5)
local List = game.Workspace:children()
local Torso = nil
local Distance = 1000000
local Temp = nil
local Human = nil
local Temp2 = nil
for x = 1, #List do
Temp2 = List[x]
if (Temp2.className == “Model”) and (Temp2 ~= script.Parent) then
Temp = Temp2:findFirstChild(“HumanoidRootPart”)
Human = Temp2:findFirstChild(“Humanoid”)
if (Temp ~= nil) and (Human ~= nil) and (Human.Health
0) then
if (Temp.Position - Position).magnitude < Distance then
Torso = Temp
Distance = (Temp.Position - Position).magnitude
end
end
end
end
return Torso
end

while true do
local target = findNearestPlayer(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end

I hope someone can help because it’s really annoying if the NPC (Aka. The Monster) crashes or being stuck.

Friendly greetings.

1 Like

Try using PathfindingService for better navigation.

1 Like

Can you make an example script of it? I am not very good at scripted hence.

Here is a basic tutorial:

1 Like

Okay, but it is a NCP and kills players so with is then the “destination”

The destination would be the Player’s HumanoidRootPart.

So that is this:

local destinations = character.HumanoidRootPart

1 Like

I would suggest you to improve your scripting skills first. Entering into Pathfinding isn’t a basic. Once you learn’t the basics, try again.