Hello, I tried making a script that makes a zombie npc follow the closest player to it and automatically update which player to follow. This is what I currently have (the script is inside of the zombie npc):
local PFS=game:GetService("PathfindingService") local Zomb=script.Parent local HRP=Zomb:WaitForChild("HumanoidRootPart") local Humanoid=Zomb:WaitForChild("Humanoid") local ClosestChar=nil local ClosestDist=10000 --Find Closest Player local function findClosestChar() ClosestChar=nil ClosestDist=10000 for i,v in pairs(game.Players:GetChildren()) do if v.Character and v.Character.HumanoidRootPart then local PlayerHRP=v.Character.HumanoidRootPart if ClosestChar==nil or (PlayerHRP.Position-HRP.Position).magnitude<ClosestDist then ClosestChar=v.Character ClosestDist=(PlayerHRP.Position-HRP.Position).magnitude end end end if ClosestChar then return ClosestChar.HumanoidRootPart.Position end end --Go To Position local function GoTo(End) local Path=PFS:CreatePath() Path:ComputeAsync(HRP.Position,End) local wayPoints=Path:getWaypoints() for i=1,5 do if wayPoints[i] then if wayPoints[i].Action==Enum.PathWaypointAction.Jump then Humanoid:ChangeState(Enum.HumanoidStateType.Jumping) end Humanoid:MoveTo(wayPoints[i].Position) Humanoid.MoveToFinished:Wait(2) end end end while true do if findClosestChar() then GoTo(findClosestChar()) else wait() end end
The script works, however, the zombie will stutter on his way to his destination and I want him to walk smoothly the entire way there. I'm mainly looking to fix my 2nd function (GoTo
), but I'm open to any suggestions on any part.