You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
For the Dummy to move to you properly and not break when you move.
What is the issue? Include screenshots / videos if possible!
He breaks and only rarely moves to you, Even then it doesn’t work.
A video: robloxapp-20220117-1842097.wmv (4.8 MB)
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
No such issue is present (At least I could find)
Code:
local Part = workspace.TestPart
local PathFindingService = game:GetService("PathfindingService")
while task.wait(0.1) do
for index, value in ipairs(game:GetService("Players"):GetPlayers()) do
local CharacterAdded = value.Character or value.CharacterAdded:Wait()
local HRP = value.Character.HumanoidRootPart
local RakeModel = Part
local Distance = (HRP.Position - RakeModel.Position).Magnitude
if Distance <= 100 then
if Distance < Part.CurrentTargetMagnitude.Value then
Part.CurrentTargetMagnitude.Value = Distance
Part.CurrentTargetCharacter.Value = value.Character
local Path = PathFindingService:CreatePath()
Path:ComputeAsync(Part.HumanoidRootPart.Position, Part.CurrentTargetCharacter.Value.HumanoidRootPart)
local Waypoints = Path:GetWaypoints()
for index, UpTo in ipairs(Waypoints) do
Part.Humanoid:MoveTo(UpTo.Position)
end
end
end
print(value, Distance)
if Distance > 100 and Part.CurrentTargetCharacter.Value then
Part.CurrentTargetCharacter.Value = nil
Part.CurrentTargetMagnitude.Value = 100
end
end
end
Here you don’t include the actual pathfinding and moving of the RakeModel, so we can’t really help you on the Pathfinding script.
The code you pasted for us, is just the code used to select a target. And on that note, I think it’s a little wrong. I would change the loop to:
while task.wait(1) do
local newTargetMagnitude = 100
local newTargetCharacter = nil
for index, value in ipairs(game:GetService("Players"):GetPlayers()) do
local CharacterAdded = value.Character or value.CharacterAdded:Wait()
local HRP = value.Character.HumanoidRootPart
local RakeModel = Part
local Distance = (HRP.Position - RakeModel.Position).Magnitude
if Distance < newTargetMagnitude then
newTargetMagnitude = Distance
newTargetCharacter = CharacterAdded
end
print(value, Distance)
end
Part.CurrentTargetMagnitude.Value = newTargetMagnitude
Part.CurrentTargetCharacter.Value = newTargetCharacter
end
If you also have the code used to follow the Pathfinding’s result and movement, that would let me help you more!
Oh! My bad. I added pathfinding to it but i can’t figure to do it. Am i missing something, and if you check the video, you’ll see what I mean.
Edit: Yeah. One of my co-workers replaced the script by accident. As i closed studio. I can’t undo it.
As is stated there was a system that got accidentally removed by a co-worker. I have a video of what was happening tho, and I may be able to recreate it.
Recreated it. Here’s the new code (I also updated the original code)
local Part = workspace.TestPart
local PathFindingService = game:GetService("PathfindingService")
while task.wait(0.1) do
for index, value in ipairs(game:GetService("Players"):GetPlayers()) do
local CharacterAdded = value.Character or value.CharacterAdded:Wait()
local HRP = value.Character.HumanoidRootPart
local RakeModel = Part
local Distance = (HRP.Position - RakeModel.Position).Magnitude
if Distance <= 100 then
if Distance < Part.CurrentTargetMagnitude.Value then
Part.CurrentTargetMagnitude.Value = Distance
Part.CurrentTargetCharacter.Value = value.Character
local Path = PathFindingService:CreatePath()
Path:ComputeAsync(Part.HumanoidRootPart.Position, Part.CurrentTargetCharacter.Value.HumanoidRootPart)
local Waypoints = Path:GetWaypoints()
for index, UpTo in ipairs(Waypoints) do
Part.Humanoid:MoveTo(UpTo.Position)
end
end
end
print(value, Distance)
if Distance > 100 and Part.CurrentTargetCharacter.Value then
Part.CurrentTargetCharacter.Value = nil
Part.CurrentTargetMagnitude.Value = 100
end
end
end
Can you test the following and let me know whether it works?
local Part = workspace.TestPart
local PathFindingService = game:GetService("PathfindingService")
while task.wait(0.2) do
local newTargetMagnitude = 100
local newTargetCharacter = nil
local Path = PathFindingService:CreatePath()
--First do a for loop, to check for the closest player that can be reached
for index, value in ipairs(game:GetService("Players"):GetPlayers()) do
local CharacterAdded = value.Character or value.CharacterAdded:Wait()
local HRP = value.Character.HumanoidRootPart
local RakeModel = Part
local Distance = (HRP.Position - RakeModel.Position).Magnitude
if Distance < newTargetMagnitude then
local succ, msg = pcall(function() -- Running a test, to see if it's first possible to get to the player
Path:ComputeAsync(Part.HumanoidRootPart.Position, value.Character.HumanoidRootPart)
end)
-- If path computes succesfully, save this magnitude and character as the new target
if succ and Path.Status == Enum.PathStatus.Success then
-- Get the path waypoints
newTargetMagnitude = Distance
newTargetCharacter = value.Character
print("Changed to target named", newTargetCharacter, "found at a distance of:", Distance)
end
end
end
--If a target character was found move towards it.
if newTargetCharacter then
--Now compute the path again to the target.
local succ, msg = pcall(function()
Path:ComputeAsync(Part.HumanoidRootPart.Position, newTargetCharacter.HumanoidRootPart)
end)
if succ and Path.Status == Enum.PathStatus.Success then
local Waypoints = Path:GetWaypoints()
Part.Humanoid:MoveTo(Waypoints[1].Position)
end
end
end
If it doesn’t, sending me the place file to work on your actual case would help me (Of course if it is okay with you), since I just coded off the top of my head, without actual testing.
It should replace the part that you’ve sent. If it’s not working, I might be missing something that you have not presented yet. I can’t really come up with the code blindly, but if you are okay with sharing a prototype of only the NPC and your current code for its movement, I can make it work!
Still doesn’t appear to work. Doesn’t calculate closest player at all.
My code:
local Part = workspace.TestPart
local PathFindingService = game:GetService("PathfindingService")
while task.wait(0.1) do
for index, value in ipairs(game:GetService("Players"):GetPlayers()) do
local CharacterAdded = value.Character or value.CharacterAdded:Wait()
local HRP = value.Character.HumanoidRootPart
local RakeModel = Part
local Distance = (HRP.Position - RakeModel.Position).Magnitude
if Distance <= 100 then
if Distance < Part.CurrentTargetMagnitude.Value then
Part.CurrentTargetMagnitude.Value = Distance
Part.CurrentTargetCharacter.Value = value.Character
local Part = workspace.TestPart
local PathFindingService = game:GetService("PathfindingService")
while task.wait(0.2) do
local newTargetMagnitude = 100
local newTargetCharacter = nil
local Path = PathFindingService:CreatePath()
--First do a for loop, to check for the closest player that can be reached
for index, value in ipairs(game:GetService("Players"):GetPlayers()) do
local CharacterAdded = value.Character or value.CharacterAdded:Wait()
local HRP = value.Character.HumanoidRootPart
local RakeModel = Part
local Distance = (HRP.Position - RakeModel.Position).Magnitude
if Distance < newTargetMagnitude then
local succ, msg = pcall(function() -- Running a test, to see if it's first possible to get to the player
Path:ComputeAsync(Part.HumanoidRootPart.Position, value.Character.HumanoidRootPart)
end)
-- If path computes succesfully, save this magnitude and character as the new target
if succ and Path.Status == Enum.PathStatus.Success then
-- Get the path waypoints
newTargetMagnitude = Distance
newTargetCharacter = value.Character
print("Changed to target named", newTargetCharacter, "found at a distance of:", Distance)
end
end
end
--If a target character was found move towards it.
if newTargetCharacter then
--Now compute the path again to the target.
local succ, msg = pcall(function()
Path:ComputeAsync(Part.HumanoidRootPart.Position, newTargetCharacter.HumanoidRootPart)
end)
if succ and Path.Status == Enum.PathStatus.Success then
local Waypoints = Path:GetWaypoints()
Part.Humanoid:MoveTo(Waypoints[1].Position)
end
end
end
end
end
print(value, Distance)
if Distance > 100 and Part.CurrentTargetCharacter.Value then
Part.CurrentTargetCharacter.Value = nil
Part.CurrentTargetMagnitude.Value = 100
end
end
end
local humanoid = script.Parent:WaitForChild("Humanoid")
local root = script.Parent:WaitForChild("HumanoidRootPart")
local pathfindingService = game:GetService("PathfindingService")
local path = pathfindingService:CreatePath()
local pos = Vector3.new() --Put your position here.
while task.wait() do
path:ComputeAsync(root.Position,pos)
if path.Status == Enum.PathStatus.Sucess then --Fix this if I made grammar error
local wps = path:GetWaypoints()
if wps[3] then
humanoid:MoveTo(wps[3].Position)
end
end
end