Hello everyone,
I have been trying to make a pathfinding Npc which follows the player when it sees him
it works perfectly but The npc moves strangely.
here is the script
local pathfinding = game:GetService("PathfindingService")
local path = pathfinding:CreatePath()
local Npc = game.Workspace.Rig
game.ReplicatedStorage.SeenRemote.OnServerEvent:Connect(function(Plr)
path:ComputeAsync(Npc.HumanoidRootPart.Position , Plr.Character.HumanoidRootPart.Position)
local Waypoints = path:GetWaypoints()
for i,waypoint in pairs(Waypoints) do
Npc.Humanoid:MoveTo(waypoint.Position)
Npc.Humanoid.MoveToFinished:Wait()
end
end)
and here is the client
local runservice = game:GetService("RunService")
local Plr = game.Players.LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:Wait()
local Npc = workspace.Rig
runservice.RenderStepped:Connect(function(A)
local PlrtoNpc = (Char.Head.Position - Npc.Head.Position).Unit
local Npclook = Npc.Head.CFrame.LookVector
local producthead = PlrtoNpc:Dot(Npclook)
if producthead > .4 then
wait(1)
game.ReplicatedStorage.SeenRemote:FireServer()
end
end)
Can you tell me where the local script is located? Because sometimes it runs twice if you place it somewhere in StarterPlayer. If it is running twice try putting it in StarterGui and then put it in a ScreenGui.
I do think itβs an issue where something is running too many times since just 1 task.wait() normally doesnβt cause any lag. But other than that iβm starting to run out of ideas.