-
What do you want to achieve? i want the NPC follow me smoothly.
-
What is the issue? i am trying to make the NPC follow me, and he does follow me, but he goes into my oldest position once i turn. When i turn left/right, he starts to stutter walk to me and he won’t get to me (im trying to make a monster that kills you at a close range)
-
What solutions have you tried so far? i tried looking for the solutions on the Developer hub, but i found nothing. I also tried SetNetworkOwner(nil) but it didn’t work.
here is the AI script for the npc:
local NPC = script.Parent
local hum = NPC.Humanoid
NPC.HumanoidRootPart:SetNetworkOwner(nil)
local PathFindingService = game:GetService("PathfindingService")
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 40
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (NPC.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function getPath(destination)
local pathParams = {
["AgentHeight"] = 8,
["AgentRadius"] = 5.1,
["AgentCanJump"] = false,
["AgentCanClimb"] = false
}
local path = PathFindingService:CreatePath(pathParams)
path:ComputeAsync(NPC.HumanoidRootPart.Position, destination.Position)
return path
end
local function Jumpscare(target)
local NPCJSC = workspace.ShadeJSC
local attackAnim1 = hum:LoadAnimation(workspace.ShadeJSC.Attack1)
local oldPosition = NPC.HumanoidRootPart.Position
local newPosition = workspace.JSCPos.Position
NPCJSC.HumanoidRootPart.Position = oldPosition
oldPosition = newPosition
attackAnim1:Play()
attackAnim1.Stopped:Wait()
target.Humanoid.Health = 0
oldPosition = NPCJSC.HumanoidRootPart.Position
NPCJSC.HumanoidRootPart.Position = newPosition
hum.WalkSpeed = 8
end
local function attack(target)
local distance = (NPC.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 8 then
hum.WalkSpeed = 12
hum:MoveTo(target.HumanoidRootPart.Position)
else
Jumpscare(target)
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target and target.Humanoid.Health > 0 then
print(target.Name)
attack(target)
break
else
print("Moving to", waypoint.Position)
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
end
end
else
hum:MoveTo(destination.Position - (NPC.HumanoidRootPart.CFrame.LookVector * 10))
end
end
local function patrol()
local waypoints = workspace.Waypoints:GetChildren()
local randomNum = math.random(1, # waypoints)
walkTo(waypoints[randomNum])
end
while wait(0.85) do
patrol()
end