i am trying to achieve:
look for nearest players of the npc and make that npc follow that player
i have no clue on how to do this some help would be appreciated thanks
i am trying to achieve:
look for nearest players of the npc and make that npc follow that player
i have no clue on how to do this some help would be appreciated thanks
Try making a script in the NPC. The NPC would have to have a pathfinding thing.
There is a video on this:
does this work for r15 too bc this is r6 unless it uses humanoid root part for distancing
You can use magnitude to get the distance between two points that help determine the player’s proximity. Once the closest player is found, you can use MoveTo to have the Npc follow then. It’s advised to have your MoveTo under a loop, so the player is always checked.
here i made the code but now i get an error here is the error
18:42:44.571 - Argument 2 missing or nil
18:42:44.573 - Stack Begin
18:42:44.577 - Script 'Workspace.MobHolder.StartingZone.Villager.Distance', Line 49
18:42:44.579 - Stack End
code
-- this will make the mob follow the player nearby i'd say by 35 centimeters
-- // services \\ --
local PathfindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
-- functions
function DetectPlayer(NPCPos)
-- only players allowed
local List = workspace:GetChildren()
-- conifig
local Distance = 50
local Torso = nil
local temp = nil
local Human = nil
local temp2 = nil
for num = 1, #List do
-- ok check distance if less then 51
temp2 = List[num]
if (temp2:IsA("Model")) and (temp2 ~= script.Parent) then
temp = temp2:FindFirstChild("HumanoidRootPart")
Human = temp2:FindFirstChild("Humanoid")
-- distance then follow
if (temp ~= nil) and (Human ~= nil) and (Human.Health > 0) then
if (temp.Position - NPCPos).Magnitude < Distance + 1 then
Torso = temp
Distance = (temp.Position - NPCPos).Magnitude
end
end
end
end
-- give it back
return Torso
end
RunService.Stepped:Connect(function()
local NearestPlayer = DetectPlayer(script.Parent.HumanoidRootPart.Position)
-- humanoid
local Humanoid = script.Parent.Humanoid
if NearestPlayer ~= nil then
-- Advanced AI
local path = PathfindingService:CreatePath()
path:ComputeAsync(script.Parent.HumanoidRootPart.Position - NearestPlayer.Position)
for _, Waypoints in pairs(path:GetWaypoints()) do
Humanoid:MoveTo(Waypoints.Position)
-- if its jump then
if Waypoints.Action == Enum.PathWaypointAction.Jump then
script.Parent.Humanoid.Jump = true
end
-- then wait until he is finished
Humanoid.MoveToFinished:Wait()
end
end
end)
it basically returns the position given of the near player hrp then move the npc there
i fixed it and dont mind i made a symbol mistake
Oh you fixed the symbol, great job!
Your method is not very feasible. Since you’re getting “HumanoidRootPart” and “Humanoid” from every model in workspace, what happens if your script gets other models that are not the player’s character model and don’t have those properties, wouldn’t it error?
It’s more convenient to loop through all the players, and get the HRP from their character.
for i, v in pairs(game.Players:GetPlayers()) do
local character = v.character
if character then
local humanoid = character.Humanoid
local hrp = character.HumanoidRootPart
I think that coding will work.
i could just do this check
if game.Players:FindFirstChild(NearestPlayer.Parent.Name) then
Oh okay @FerbZides you can.