I want the npc to chase the player after it picks up the item and for the first time it works great, but when trying it again there’s a lot of weird things going on, the npc moves slower (the WalkSpeed should be 40), and it never kills the players etc etc. here’s a video demonstrating it
Here’s the code for the pathfinding
function chasePlayer(player, monster, monsterSettings)
if not player.Character then return end
if player.Character.Humanoid.Health <= 0 then return end
local path = PathfindingService:CreatePath({
AgentCanJump = true
})
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {player.Character, workspace.SpecialItems:GetDescendants(), monster, wp}
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
local ray = workspace:Raycast(player.Character.PrimaryPart.Position, player.Character.PrimaryPart.CFrame.UpVector * -500, rayParams)
if not ray then return end
if ray.Instance.Name ~= "Baseplate" then print(ray.Instance.Name) end
local endPos = ray.Position
local success, err = pcall(function()
path:ComputeAsync(monster.PrimaryPart.Position, endPos)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
if showWaypoints then
if len(wp) > 0 then
for i, v in pairs(wp) do
v:Destroy()
end
end
wp = {}
for i, v in pairs(waypoints) do
local part = Instance.new("Part")
part.Position = v.Position
part.Size = Vector3.new(0.5, 0.5, 0.5)
part.CanCollide = false
part.Anchored = true
part.Name = "Waypoint"
part.Parent = workspace
table.insert(wp, part)
end
end
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
if blockedWaypointIndex >= nextWaypointIndex then
blockedConnection:Disconnect()
chasePlayer(player, monster, monsterSettings)
end
end)
if not reachedConnection then
reachedConnection = monster.Humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
local dist = (monster.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude
if dist >= monsterSettings.KillDistance then
nextWaypointIndex += 1
monster.Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
player.Character.Humanoid:TakeDamage(100)
reachedConnection:Disconnect()
blockedConnection:Disconnect()
end
else
player.Character.Humanoid:TakeDamage(100)
reachedConnection:Disconnect()
blockedConnection:Disconnect()
end
end)
end
nextWaypointIndex = 2
monster.Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
warn("Path not computed "..err)
end
end
This is what triggers the ai chase and what happens after it stops
-- THE TASK.SPAWN IS IN A PART UNDER FUNCTION fireScript()
task.spawn(function()
rsConnection = game:GetService("RunService").Stepped:Connect(function()
if char.Humanoid.Health > 0 then
chasing = true
if chasing then chasePlayer(player, monster, monsterSettings) end
else
rsConnection:Disconnect()
chasing = false
cleanup(monster, grip, monsterSettings)
end)
end)
end
function cleanup(monster, grip, monsterSettings)
nextWaypointIndex = 2
if blockedConnection then blockedConnection:Disconnect() blockedConnection = nil end
if reachedConnection then reachedConnection:Disconnect() reachedConnection = nil end
if rsConnection then rsConnection:Disconnect() rsConnection = nil end
task.wait(monsterSettings.SleepTime)
fireScript()
end
Any help is appreciated, I don’t know what’s causing it