Bot still trying to go trought wall

Hello the, my fellas,

I am trying to make a chasing AI bot using my own script, I used the PathfindingService feature for the first time. I got confuse, why did it still trying to go trought wall? I already use path params but still didn’t work. Hope you guys can help

AI:

local PathfindingService = game:GetService("PathfindingService")
local CollectionService = game:GetService("CollectionService")
local PhysicsService = game:GetService("PhysicsService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Modules = ReplicatedStorage:WaitForChild("Modules")

local killer = script.Parent
local rootPart = killer.HumanoidRootPart
local humanoid = killer.Humanoid

for index, part in pairs(killer:GetChildren()) do
	if part:IsA("BasePart") then
		PhysicsService:SetPartCollisionGroup(part, "KillerBot")
	end
end

local function searchForPath(object)
	
	local pathParams = {
		["AgentHeight"] = 10,
		["AgentRadius"] = 5,
		["AgentCanJump"] = true
	}
	
	local path = PathfindingService:CreatePath(pathParams)
	path:ComputeAsync(rootPart.Position, object.Position)
	
	return path, path:GetWaypoints()
end

local function getPlayer()
	local players = Players:GetPlayers()
	local maxDistance = 100
	local nearestTarget
	
	local plr
	local char
	
	for index, player in pairs(players) do
		plr = player
		if player.Character then
			char = player.Character
			local target = char
			local distance = (rootPart.Position - target.HumanoidRootPart.Position).magnitude
			if distance > maxDistance then
				maxDistance = distance
				nearestTarget = target
			end
		else
			char = nil
		end
	end
	
	return nearestTarget, plr, char
end

local function randomMovement()
	local number = 2
	local randomNumber = math.random(1, number)
	local destination = Vector3.new(randomNumber, 0, randomNumber)
	
	humanoid:MoveTo(destination)
end

local function patrolMovement()
	local target, player, character = getPlayer()
	local path, waypoints
	local characterStatus
	
	if character ~= nil then
		path, waypoints = searchForPath(character.HumanoidRootPart)
		characterStatus = true
	else
		path, waypoints = searchForPath(rootPart)
		characterStatus = false
	end
	
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(waypoints) do
			if characterStatus == true then
				humanoid:MoveTo(character.HumanoidRootPart.Position)
			else
				randomMovement()
			end
		end
	else
		randomMovement()
	end
	
end

while wait(0) do
	patrolMovement()
end

I still can’t figure out how to do it

Can you send a video showing the problem? Also, are you sure it’s trying to pathfind and not doing randomMovement()?

I mean like, the bot do chasing me/player but it still try chasing me trought wall

you’re not actually pathfinding to the waypoint in your for loop. you are trying to move to character.HumanoidRootPart.Position instead of the waypoint.

2 Likes

Like RecruitDeity said above
Instead of doing character.HumanoidRootPart.Position you should do waypoint.Position so you actually walk to the waypoint instead of the player rootpart position

I change it to humanoid:MoveTo(waypoint.Position), but it still didn’t work
image

That’s because you’re not waiting for the humanoid to finish moving to the point and then walk to the next point, to fix this you have to add the event .MoveToFinished

local function patrolMovement()
	local target, player, character = getPlayer()
	local path, waypoints
	local characterStatus
	
	if character ~= nil then
		path, waypoints = searchForPath(character.HumanoidRootPart)
		characterStatus = true
	else
		path, waypoints = searchForPath(rootPart)
		characterStatus = false
	end
	
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(waypoints) do
			if characterStatus == true then
				humanoid:MoveTo(character.HumanoidRootPart.Position)
humanoid.MoveToFinished:Wait() -- Waits for the humanoid to finish walking to the point
			else
				randomMovement()
			end
		end
	else
		randomMovement()
	end
	
end

Sorry for the bad formatting I am on mobile, you can fix it after.

image
still

nvm, I changed from character.HumanoidRootPart.Position to MoveTo(waypoint.Position)

1 Like