AI not moving, code is executed more than once?

Hello, I’m trying to make a pathfinding monster, and so far I have a very basic script. However, once the code runs, the functions are executed multiple times, and It doesn’t move the AI at all. I’ve tried printing the number of targets, which it won’t do.

PathFindingService = game:GetService("PathfindingService")
wait(4)
function FindNearest()
	local player = game.Players:GetPlayers()
	local position = workspace.Ray1.Position
    local lowest = math.huge 
    local NearestPlayer = nil
    for i,v in pairs(game.Players:GetPlayers()) do
        if v.Character then
            local distance = v:DistanceFromCharacter(position)
            if distance < lowest then
                lowest = distance
                NearestPlayer = v
            end
        end
    end
	GoToTarget()
    return NearestPlayer
end


function GoToTarget()
	
	local Player = FindNearest()
	local path = PathFindingService:CreatePath()
	local HumanoidRootPart = script.Parent.HumanoidRootPart
	local TargetCharacter = Player.Character
	path:ComputeAsync(HumanoidRootPart.Position,TargetCharacter)
	local wayPoints = path:GetWayPoints()
	script.Parent.Humanoid:MoveTo(wayPoints.Position)
end

FindNearest()
---I only call FindNearest once, which calls GoToTarget() ONCE

This is what the output looks like
https://gyazo.com/af4bc2aebf1acbe82ad9dda2cb777125

1 Like

You’re calling “GoToTarget()” inside a function which then calls FindNearest() and so forth… It’s basically an infinite loop (except it breaks due to stack overflow)

Is this the line that’s calling FindNearest()? I thought this line was setting “player” to “NearestPlayer”.

That alone is fine. It’s when the FindNearest() function is called is when it calls GoToTarget(), which then calls FindNearest() and so forth. To fix it, simply remove the “GoToTarget” call inside the FindNearest function.

You have multiple issues with your code, the reason it’s repeating multiple times is due to GoToTarget specifically local Player = FindNearest(), it calls FindNearest which then calls GoToTarget again and so on.

The other one is to do with GetWayPoints, it isn’t actually a function but im guessing you meant GetWaypoints, once that is replaced we are left with the issue that it returns an array and not a single object which means we have to iterate through it instead.

We also have to replace path:ComputeAsync(HumanoidRootPart.Position,TargetCharacter) with path:ComputeAsync(HumanoidRootPart.Position, TargetCharacter.HumanoidRootPart.Position) due to the fact that the function expects a Vector3 and we are passing an Instance.

Once all of this and some other things are cleaned up we end up with something looking like this:

local PathFindingService = game:GetService("PathfindingService")

function FindNearest()
	local player = game:GetService("Players"):GetPlayers()
	local position = game:GetService("Workspace").Ray1.Position
	local lowest = math.huge 
	local NearestPlayer = nil

    for i,v in pairs(game.Players:GetPlayers()) do
        if v.Character then
            local distance = v:DistanceFromCharacter(position)
            if distance < lowest then
                lowest = distance
                NearestPlayer = v
            end
        end
    end

    return NearestPlayer
end


function GoToTarget()
	
	local player = FindNearest()
	if (not player) then
		wait(1)
		GoToTarget()
		return
	end
	
	local path = PathFindingService:CreatePath()
	local HumanoidRootPart = script.Parent.HumanoidRootPart
	local TargetCharacter = player.Character
	path:ComputeAsync(HumanoidRootPart.Position, TargetCharacter.HumanoidRootPart.Position)
	
	local waypoints = path:GetWaypoints()
	
	for _,v in pairs(waypoints) do
		script.Parent.Humanoid:MoveTo(v.Position)
		script.Parent.Humanoid.MoveToFinished:Wait()
	end
	
end

GoToTarget()

Keep in mind that this is only executed once, if you want it to keep repeating then you’re going to have to put it in a loop.