-
okay so i want to make an NPC that is able to follow the nearest players while also avoiding walking into walls and being able to jump over anything that might block its path (like parts or what not)
-
i have a script that does exactly that BUT it is very slow, i made it in a way it creates waypoints to the players location and then it moves to that waypoint and creates a new one, etc etc etc. problem is, the player is always moving and it makes the NPC just look kinda dumb moving to the last “known/created” waypoint
-
i’m not amazing or great or generally good with making such scripts. i have used the ai assistance and looking up a few things, a friend of mine said i’d be good to use raycasting but idk anything about that
below here is the script that i use for the NPC to create waypoints and basically just "follow’ the player.
Currently i have the human.MoveToFinished:Wait()
because i was kinda getting lost and figured why not disable it for the NPC to get to the finish and just go straight for the player, but that just made it like any other simple follow script.
some variables
local human = script.Parent:WaitForChild(“Humanoid”)
local torso = script.Parent:WaitForChild(“Torso”)
local function FollowNearestPlayer()
local nearestPlayer = getNearestPlayer()
if nearestPlayer and nearestPlayer.Character and nearestPlayer.Character:FindFirstChild("HumanoidRootPart") then
human.WalkSpeed = normalWalkSpeed
local targetPosition = nearestPlayer.Character.HumanoidRootPart.Position
local path = PathfindingService:CreatePath()
path:ComputeAsync(torso.Position, targetPosition)
local waypoints = path:GetWaypoints()
path.Blocked:Connect(function()
print("sum is blocking its path")
end)
for i, waypoint in waypoints do
if waypoint.Action == Enum.PathWaypointAction.Jump then
human:ChangeState(Enum.HumanoidStateType.Jumping)
wait(1)
end
human:MoveTo(waypoint.Position)
-- human.MoveToFinished:Wait()
end
else
human.WalkSpeed = 0
if human.WalkSpeed > 0 then
error("NPC didnt change speed")
end
end
end