Hello devforum, I have a script that I’ve been working on for a horror game that doesn’t function the way I want it to.
For context, the script is for a humanoid AI, and is mainly to allow it to chase after players when close enough. The main aspects I want to achieve with it is to have it pathfind around geometry so that it can properly reach players, but more notably, update the path frequently (about once every .1 seconds) to more accurately chase the player, since said player is most likely going to be moving, sometimes a lot.
However, the issue that arises is that the AI just doesn’t move at all, and in previous attempts to fix the script, it moves for .1 seconds, but then stops. There aren’t even any errors that appear in the output, the AI just completely breaks.
Despite my best efforts, the outcome has remained pretty much the same; works for a bit, but breaks not even a second later.
local function FindNearestPlayer()
local chaseRadius = script.Parent.Configuration.ChaseRadius
local nearestPlayer = nil
local nearestDistance = math.huge
for _, player in ipairs(game.Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local playerPosition = player.Character.HumanoidRootPart.Position
local distance = (script.Parent.HumanoidRootPart.Position - playerPosition).Magnitude
if distance <= chaseRadius.Value and distance < nearestDistance then
nearestPlayer = player
nearestDistance = distance
end
end
end
return nearestPlayer
end
local function Chase(nearestPlayer)
if nearestPlayer then
script.Parent.Humanoid.WalkSpeed = script.Parent.Configuration.RunSpeed.Value
local path = game:GetService("PathfindingService"):CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false,
AgentMaxSlope = 45,
})
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, nearestPlayer.Character.HumanoidRootPart.Position)
while path.Status ~= Enum.PathStatus.Success do
task.wait(.1)
return Chase(FindNearestPlayer())
end
for _, waypoint in ipairs(path:GetWaypoints()) do
script.Parent.Humanoid:MoveTo(waypoint.Position)
end
end
end
local function Chasing()
while true do
local nearestPlayer = FindNearestPlayer()
Chase(nearestPlayer)
end
end
script.Parent.Head.ScreamA:Play()
script.Parent.Head.ScreamB:Play()
Chasing()
Any kind of help would be appreciated, as this issue has stumped me for quite some time now.