My Monster AI Isn't Chasing me Correctly

Video

robloxapp-20240719-1753242.wmv (5.8 MB)

Here is the AI Script

local Monster = script.Parent
local humanoid = Monster.Humanoid
local pathfindingService = game:GetService(“PathfindingService”)
Monster.PrimaryPart:SetNetworkOwner(nil)

local Track = humanoid:LoadAnimation(script.WalkAnim)
Track.Looped = true
Track:Play()

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

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

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 = 50
local nearestTarget

for index, player in pairs(players) do
	if player.Character then
		local target = player.Character
		local distance = (Monster.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 pathParams = {
[“AgentHeight”] = 5,
[“AgentRadius”] = 4,
[“AgentCanJump”] = false
}

local path = pathfindingService:CreatePath(pathParams)
path:ComputeAsync(Monster.HumanoidRootPart.Position, destination.Position)

return path

end

local function attack(target)
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
			attack(target)
			break
		else
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
		end
	end
else
	humanoid:MoveTo(destination.Position - (Monster.HumanoidRootPart.CFrame.LookVector * 10))
end

end

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

while wait(0.5) do
patrol()
end

Here is the Game

I just want it to chase me, and when it stops chasing, it goes back to wandering around.

It seems like your code isn’t formatted correctly. Which can cause problems.

Here it is correctly formatted for others to read:

local Monster = script.Parent
local humanoid = Monster.Humanoid
local pathfindingService = game:GetService("PathfindingService")
Monster.PrimaryPart:SetNetworkOwner(nil)

local Track = humanoid:LoadAnimation(script.WalkAnim)
Track.Looped = true
Track:Play()

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

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

	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 = 50
	local nearestTarget

	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (Monster.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 pathParams = {
		["AgentHeight"] = 5,
		["AgentRadius"] = 4,
		["AgentCanJump"] = false
	}

	local path = pathfindingService:CreatePath(pathParams)
	path:ComputeAsync(Monster.HumanoidRootPart.Position, destination.Position)

	return path

end

local function attack(target)
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
				attack(target)
				break
			else
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(destination.Position - (Monster.HumanoidRootPart.CFrame.LookVector * 10))
	end

end

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

while wait(0.5) do
	patrol()
end
1 Like

Sorry, I’m new to roblox development, What do you mean by “code isn’t formatted correctly”?

There’s nothing wrong with your code, the way its shown is what’s going wrong.

In your post, the code is being split being a paragraph to being back to code, and it’s cycling between them.

Ohhh, sorry i didnt know how to do the code in the section, thanks for that, I am also new to devforum, do you have any solutions for the bug though? If not its perfectly fine

By the way If there are any tutorials on youtube or models I can use to replace that current AI System That uses waypoints I would gladly do that