Anyone know why my ai is confused?

I have 2 moving ais using the same extract script and one of to me is confused which way point to go first to so it goes back and forth, maybe should I make them use separate way points?

your video is not showing anything

1 Like

Let me resend and I just created a 2nd waypoint named waypoint2 and changed the script and it’s still confused

I still don’t see anything. Could I see the scripts?

Well ima describe the video for you so the monster is in the middle of both waypoints and it keeps turning towards one way point then decides to go the other way point and over and over so it doesn’t get anywhere let me send the pic of the scripts Brb

It won’t let me reply on pc I have no button to reply

sorry i dont know why it sends dups

it should only be 3 pics but it sends 2 for some reason

You can copy and paste all your script then use ``` to enter script mode and paste it all in there
like this

print ("btw don't forget when putting the ``` it doesn't work unless you put it on a new line")

Also I can barely see your scripts. So could you copy and paste all your code?

Never knew that let me do it rn

"local teddy = script.Parent
local humanoid = teddy.Humanoid
teddy.PrimaryPart:SetNetworkOwner(nil)

local function canSeeTarget(target)
local origin = teddy.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - teddy.HumanoidRootPart.Position).unit * 230
local ray = Ray.new(origin, direction)

local hit, pos = workspace:FindPartOnRay(ray, teddy)


if hit then
	if hit:IsDescendantOf(target) then
		return true
	end
else
	return false
end

end

local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 390
local nearestTarget

for index, player in pairs(players) do
	if player.Character then
		local target = player.Character
		local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
		
		if distance < maxDistance and canSeeTarget(target) then
			nearestTarget = target
			maxDistance = distance
		end
	end
end

return nearestTarget

end

local function getPath(destination)
local PathfindingService = game:GetService(“PathfindingService”)

local pathParams = {
	["AgentHeight"] = 15,
	["AgentRadius"] = 4,
	["AgentCanJump"] = false
}

local path = PathfindingService:CreatePath(pathParams)

path:ComputeAsync(teddy.HumanoidRootPart.Position, destination.Position)

return path

end

local function attack(target)
local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

if distance > 70 then
	humanoid:MoveTo(target.HumanoidRootPart.Position)
else
	local attackAnim = humanoid:LoadAnimation(script.Attack)
	attackAnim:Play()
	target.Humanoid.Health = 0
end

end

local function walkTo(destination)

local path = getPath(destination)

if path.Status == Enum.PathStatus.Success then
	for index, waypoint in pairs(path:GetWaypoints()) do
		local target = findTarget()
		if target and target.Humanoid.Health > 0 then
			print("TARGET FOUND", target.Name)
			attack(target)
			break
		else
			print("Moving to ", waypoint.Position)
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
		end
	end
else
	humanoid:MoveTo(destination.Position - (teddy.HumanoidRootPart.CFrame.LookVector * 10))
end

end

function patrol()
local waypoints = workspace.waypoints1:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end

while wait(0.1) do
patrol()
end"

if i did it wrong tell me i will redo

local humanoid = teddy.Humanoid
teddy.PrimaryPart:SetNetworkOwner(nil)

local function canSeeTarget(target)
local origin = teddy.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - teddy.HumanoidRootPart.Position).unit * 230
local ray = Ray.new(origin, direction)

local hit, pos = workspace:FindPartOnRay(ray, teddy)


if hit then
	if hit:IsDescendantOf(target) then
		return true
	end
else
	return false
end
end

local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 390
local nearestTarget

for index, player in pairs(players) do
	if player.Character then
		local target = player.Character
		local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
		
		if distance < maxDistance and canSeeTarget(target) then
			nearestTarget = target
			maxDistance = distance
		end
	end
end

return nearestTarget
end

local function getPath(destination)
local PathfindingService = game:GetService(“PathfindingService”)

local pathParams = {
	["AgentHeight"] = 15,
	["AgentRadius"] = 4,
	["AgentCanJump"] = false
}

local path = PathfindingService:CreatePath(pathParams)

path:ComputeAsync(teddy.HumanoidRootPart.Position, destination.Position)

return path
end

local function attack(target)
local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

if distance > 70 then
	humanoid:MoveTo(target.HumanoidRootPart.Position)
else
	local attackAnim = humanoid:LoadAnimation(script.Attack)
	attackAnim:Play()
	target.Humanoid.Health = 0
end
end

local function walkTo(destination)

local path = getPath(destination)

if path.Status == Enum.PathStatus.Success then
	for index, waypoint in pairs(path:GetWaypoints()) do
		local target = findTarget()
		if target and target.Humanoid.Health > 0 then
			print("TARGET FOUND", target.Name)
			attack(target)
			break
		else
			print("Moving to ", waypoint.Position)
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
		end
	end
else
	humanoid:MoveTo(destination.Position - (teddy.HumanoidRootPart.CFrame.LookVector * 10))
end
end

function patrol()
local waypoints = workspace.waypoints1:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end

while wait(0.1) do
patrol()
end

what is destination?

path:ComputeAsync(teddy.HumanoidRootPart.Position, destination.Position)

maybe put:

path:ComputeAsync(teddy.HumanoidRootPart.Position, target.Head.Position)

idk i dident make the script think it means waypoint

maybe put:

path:ComputeAsync(teddy.HumanoidRootPart.Position, target.Head.Position)

Instead of the destination part

I wonder why the other ai moves fine with the same extract script tho

You could get the zombie model in the toolbox and just switch what the body looks like.