It our game, we have an npc that the nearest player that is inside of the maze. However, sometimes it backtracks or just completletly stops.
This npc is crucial to our game, and if we don’t fix this, we cannot sponsor our game.
This is the code that controls the npc. It uses the SimplePath module instead of the normal Roblox pathfinding.
task.wait(0.5)
local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)
--Define npc
local Dummy = script.Parent
local closestPlayer = nil
local closestMag = math.huge
-- Define a part called "Goal"
local agentParams = {
AgentRadius = 2,
AgentHeight = 8,
AgentCanClimb = true,
AgentCanJump = true,
}
--Create a new Path using the Dummy
local Path = SimplePath.new(Dummy, agentParams)
--Helps to visualize the path
Path.Visualize = false
local Goal = Vector3.new(0,0,0)
Path.Blocked:Connect(function()
print("blocked")
for i, plr in pairs(game.Players:GetChildren()) do
local inPlim = false
for i, plimObj in pairs(game.ServerStorage.PLIM:GetChildren()) do
if plimObj.Value == plr.Name then
inPlim = true
end
end
if game.Workspace:FindFirstChild(plr.Name) and game.Workspace:FindFirstChild(plr.Name):FindFirstChild("HumanoidRootPart") and inPlim then
if math.abs(game.Workspace:FindFirstChild(plr.Name):WaitForChild("HumanoidRootPart").Position.Magnitude - script.Parent:WaitForChild("HumanoidRootPart").Position.Magnitude) < closestMag or closestPlayer == nil then
closestMag = math.abs(game.Workspace:FindFirstChild(plr.Name):WaitForChild("HumanoidRootPart").Position.Magnitude - script.Parent:WaitForChild("HumanoidRootPart").Position.Magnitude)
closestPlayer = plr
end
end
end
if game.Workspace:FindFirstChild(closestPlayer.Name) then
Goal = game.Workspace:FindFirstChild(closestPlayer.Name).HumanoidRootPart
end
Path:Run(Goal)
end)
--If the position of Goal changes at the next waypoint, compute path again
Path.WaypointReached:Connect(function()
print("waypoint")
for i, plr in pairs(game.Players:GetChildren()) do
local inPlim = false
for i, plimObj in pairs(game.ServerStorage.PLIM:GetChildren()) do
if plimObj.Value == plr.Name then
inPlim = true
end
end
if game.Workspace:FindFirstChild(plr.Name) and game.Workspace:FindFirstChild(plr.Name):FindFirstChild("HumanoidRootPart") and inPlim then
if math.abs(game.Workspace:FindFirstChild(plr.Name):WaitForChild("HumanoidRootPart").Position.Magnitude - script.Parent:WaitForChild("HumanoidRootPart").Position.Magnitude) < closestMag or closestPlayer == nil then
closestMag = math.abs(game.Workspace:FindFirstChild(plr.Name):WaitForChild("HumanoidRootPart").Position.Magnitude - script.Parent:WaitForChild("HumanoidRootPart").Position.Magnitude)
closestPlayer = plr
end
end
end
if game.Workspace:FindFirstChild(closestPlayer.Name) then
Goal = game.Workspace:FindFirstChild(closestPlayer.Name).HumanoidRootPart
end
Path:Run(Goal)
end)
--Dummmy knows to compute path again if an error occurs
Path.Error:Connect(function(errorType)
print("golem error")
for i, plr in pairs(game.Players:GetChildren()) do
local inPlim = false
for i, plimObj in pairs(game.ServerStorage.PLIM:GetChildren()) do
if plimObj.Value == plr.Name then
inPlim = true
end
end
if game.Workspace:FindFirstChild(plr.Name) and game.Workspace:FindFirstChild(plr.Name):FindFirstChild("HumanoidRootPart") and inPlim then
if math.abs(game.Workspace:FindFirstChild(plr.Name):WaitForChild("HumanoidRootPart").Position.Magnitude - script.Parent:WaitForChild("HumanoidRootPart").Position.Magnitude) < closestMag or closestPlayer == nil then
closestMag = math.abs(game.Workspace:FindFirstChild(plr.Name):WaitForChild("HumanoidRootPart").Position.Magnitude - script.Parent:WaitForChild("HumanoidRootPart").Position.Magnitude)
closestPlayer = plr
end
end
end
if game.Workspace:FindFirstChild(closestPlayer.Name) then
Goal = game.Workspace:FindFirstChild(closestPlayer.Name).HumanoidRootPart
end
Path:Run(Goal)
end)
Path.Reached:Connect(function()
for i, plr in pairs(game.Players:GetChildren()) do
local inPlim = false
for i, plimObj in pairs(game.ServerStorage.PLIM:GetChildren()) do
if plimObj.Value == plr.Name then
inPlim = true
end
end
if game.Workspace:FindFirstChild(plr.Name) and game.Workspace:FindFirstChild(plr.Name):FindFirstChild("HumanoidRootPart") and inPlim then
if math.abs(game.Workspace:FindFirstChild(plr.Name):WaitForChild("HumanoidRootPart").Position.Magnitude - script.Parent:WaitForChild("HumanoidRootPart").Position.Magnitude) < closestMag or closestPlayer == nil then
closestMag = math.abs(game.Workspace:FindFirstChild(plr.Name):WaitForChild("HumanoidRootPart").Position.Magnitude - script.Parent:WaitForChild("HumanoidRootPart").Position.Magnitude)
closestPlayer = plr
end
end
end
if game.Workspace:FindFirstChild(closestPlayer.Name) then
Goal = game.Workspace:FindFirstChild(closestPlayer.Name).HumanoidRootPart
end
Path:Run(Goal)
end)
for i, plr in pairs(game.Players:GetChildren()) do
local inPlim = false
for i, plimObj in pairs(game.ServerStorage.PLIM:GetChildren()) do
if plimObj.Value == plr.Name then
inPlim = true
end
end
if game.Workspace:FindFirstChild(plr.Name) and game.Workspace:FindFirstChild(plr.Name):FindFirstChild("HumanoidRootPart") and inPlim then
if math.abs(game.Workspace:FindFirstChild(plr.Name):WaitForChild("HumanoidRootPart").Position.Magnitude - script.Parent:WaitForChild("HumanoidRootPart").Position.Magnitude) < closestMag or closestPlayer == nil then
closestMag = math.abs(game.Workspace:FindFirstChild(plr.Name):WaitForChild("HumanoidRootPart").Position.Magnitude - script.Parent:WaitForChild("HumanoidRootPart").Position.Magnitude)
closestPlayer = plr
end
end
end
if game.Workspace:FindFirstChild(closestPlayer.Name) then
Goal = game.Workspace:FindFirstChild(closestPlayer.Name).HumanoidRootPart
end
Path:Run(Goal)
Ask any questions about the script if anything isn’t clear!
Thank you!
~ NinjaFactory