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!
I want to create a monster pathfinding ai that can hear things and chase you when found.
- What is the issue? Include screenshots / videos if possible!
Everything works, except if the script is told to move multiple times the rig starts to “limp.” It makes it to the required target but with a ton of stopping in between.
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried using debounce methods or some other ways to hopefully stall other calls, but it doesn’t seem to work. The script that I am about to show you may have some failed attempts at this.
I didn’t find any devhub or devforum article this obscure, so no.
local pathservice = game:GetService("PathfindingService")
local subject = script.Parent:FindFirstChild("Humanoid")
local repstorage = game:GetService("ReplicatedStorage")
local event = repstorage:WaitForChild("Noise")
local path = pathservice:CreatePath()
local aggro = false
local activect = nil
local heard = false
local NPC = script.Parent
local players = game.Players:GetPlayers()
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
local target = nil
--I hear you
--DevForum, For testing purpose the NPC "hears" you when I step on a part
event.OnServerEvent:Connect(function(plr, pos, range)
if not heard then heard = true
if (subject.Parent.HumanoidRootPart.Position - pos).Magnitude <= range then
path:ComputeAsync(subject.Parent.HumanoidRootPart.Position, pos)
local wp = path:GetWaypoints()
table.remove(wp, 1)
for i, v in pairs (wp) do
if aggro then return end
subject:MoveTo(v.Position)
if v.Action == Enum.PathWaypointAction.Jump then
subject.Jump = true
end
subject.MoveToFinished:Wait()
end
heard = false
end
end
print("finished")
end)
--I see you
--DevForum, It keeps doing that strange movement pattern as long as I'm in sight,
--otherwise it's perfectly fine.
coroutine.wrap(function()
print("OY")
while true do
wait(1/4)
if aggro then
update()
repeat task.wait() until aggro == true
end
end
end)()
coroutine.wrap(function()
while true do
local players = game.Players:GetPlayers()
for i, v in pairs (players) do
local char = v.Character
if not char then continue end
local look = NPC.HumanoidRootPart.CFrame.LookVector
local facing_direction = (char.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position).Unit
local dot = look:Dot(facing_direction)
local angle = math.deg(math.acos(dot))
if angle <= 80 then
local rayresult = workspace:Raycast(NPC.HumanoidRootPart.Position, (char.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position)*100)
if rayresult.Instance.Parent == char then
aggro = true
target = char.HumanoidRootPart.Position
end
else
aggro = false
target = nil
end
end
task.wait(1/4)
end
end)()
function update()
path:ComputeAsync(subject.Parent.HumanoidRootPart.Position, target)
local wp = path:GetWaypoints()
table.remove(wp, 1)
coroutine.wrap(startwalking)(wp)
end
function startwalking (wp)
activect = coroutine.running()
for i, v in pairs (wp) do
if activect ~= coroutine.running() then return end
subject:MoveTo(v.Position)
print("moving")
if v.Action == Enum.PathWaypointAction.Jump then
subject.Jump = true
end
subject.MoveToFinished:Wait()
end
end
This is the only script that you will need, its unnecessary to ask for the other scripts…