Pathfinding script that you can use

Hello, I’m not the best at scripting and I have had some help with this script, however with help I was able to get it working in my game. I don’t expect anybody to see this post or to care about it. But I wanted to share it anyways because it might help someone. Credit isn’t required but you can credit me if you’d like


local PathfindingService = game:GetService("PathfindingService")

local AI = script.Parent
local RP = script.Parent.HumanoidRootPart
local Hum = script.Parent.Humanoid

local Waypoints = game.Workspace:WaitForChild("Waypoints"):GetChildren()

RP:SetNetworkOwner(nil)

local attackAnim = Hum.Animator:LoadAnimation(script.Attack)
local walkAnim = Hum.Animator:LoadAnimation(script.Walk)
local runAnim = Hum.Animator:LoadAnimation(script.Run)

local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = {Hum}

local Damage = 25
local AttackRange = 5


walkAnim.Looped = true
runAnim.Looped = true
walkAnim:Play()


local pathParams = {
	AgentHeight = 5,
	AgentRadius = 3,
	AgentCanJump = true,
}

local function getPath(destination)
	local path = PathfindingService:CreatePath(pathParams)

	path:ComputeAsync(RP.Position, destination)

	return path	
end

function getTarget()

	local closestTarget
	local distanceFromClosestTarget = 1000000000

	for i, player in pairs(game.Players:GetChildren()) do
		local distance = (player.Character.HumanoidRootPart.Position - RP.Position).Magnitude

		if distance < distanceFromClosestTarget then
			distanceFromClosestTarget = distance
			closestTarget = player
		end
	end


	return(closestTarget)
end

local db = false

local runAnimPlayingStatus = false
local walkAnimPlayingStatus = false

function chaseTarget(target)
	if runAnimPlayingStatus == false then
		walkAnim:Stop()
		runAnim:Play()
		runAnimPlayingStatus = true
		walkAnimPlayingStatus = false
	end
	print("chase started")
	local path
	
	path = getPath(target.Character.HumanoidRootPart.Position)
	
	local waypoints = path:GetWaypoints()
	
		Hum:MoveTo(waypoints[2].Position)
	
		
	--if (target.Character.HumanoidRootPart.Position - RP.Position).Magnitude <= 45 and target.Character.Humanoid.Health > 0 then
		--local path = PathfindingService:CreatePath(pathParams)
		--path:ComputeAsync(RP.Position, target.Character.HumanoidRootPart.Position)
		if (target.Character.HumanoidRootPart.Position - RP.Position).Magnitude <=5 then
			if not db then
				db = true
				attackAnim:Play()
				target.Character.Humanoid.Health -= Damage
				task.wait(0.5)
				db = false
			end
		end
	--else
	--	return
	--end
	
end


function moveTo(target)
	local path = getPath(target)

	if path.Status == Enum.PathStatus.Success then
		for i, waypoint in pairs(path:GetWaypoints()) do

			local target = getTarget()
			if target and (target.Character.HumanoidRootPart.Position - RP.Position).Magnitude <= 45 and target.Character.Humanoid.Health > 0 then
				path:Destroy()
				chaseTarget(target)
				break
			else
				if walkAnimPlayingStatus == false then
					walkAnim:Play()
					walkAnimPlayingStatus = true
				end
				runAnimPlayingStatus = false
				runAnim:Stop()
				Hum:MoveTo(waypoint.Position)
				Hum.MoveToFinished:Wait()

			end
		end



	end
end


function patrol()
	print("fired")
	local ChosenWapoint = math.random(1, #Waypoints)
	moveTo(game.Workspace.Waypoints:FindFirstChild(ChosenWapoint).Position)
end



while task.wait(0.2) do
	patrol()
end

I believe what the script needs is pretty self explanatory . I have a regular 2016 Mesh avatar as the dummy for who the script is running on, everything else the script requires is obvious by reading the script. If you need help, ask me. Anything that is tagged out in the script isn’t necessary and I kept it in as to avoid deleting it just in case.

7 Likes

Pretty nice man! I’m new to development so this is impressive. And don’t worry you will improve until people care

1 Like