Alright, in the past like a few days ago, I asked a question on how to fix my AI when it gets stuck. But since I never got an answer on it, I decided to try to make it work myself, and it still doesn't work. I have the entire script in the other question, I'll link the other question in the comments. If you need the entire script, I'll edit this question with the entire code, but here is what I added.

what I added in attempt to fix:


local function checkIfStuck(destination)
	local path = getPath(destination)
	
	local orgin = figure:WaitForChild("HumanoidRootPart")
	local direction = (figure.HumanoidRootPart.CFrame.LookVector).unit * 1
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, figure)
	
	if hit:IsA("Part") then
		print("STUCK LOL")
		humanoid:MoveTo(destination.Position - (figure.HumanoidRootPart.CFrame.LookVector / 5))
		humanoid.Jump = true
	end
end

entire script:


local figure = script.Parent
local humanoid = figure:WaitForChild("Humanoid")
local humanoidRootPart = figure:WaitForChild("HumanoidRootPart")
local waypoints = game.Workspace.waypoints:GetChildren()
local PathfindingService = game:GetService("PathfindingService")
local currentDestination = figure:WaitForChild("CurrentDestination")
figure.PrimaryPart:SetNetworkOwner(nil)


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

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


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

	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (figure.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

			if distance < maxDistance and canSeeTarget(target) then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end
	return nearestTarget
end

function getPath(destination)
	local pathParams = {
		["AgentHeight"] = 5,
		["AgentRadius"] = 2,
		["AgentCanJump"] = true
	}
	local path = PathfindingService:CreatePath(pathParams)
	path:ComputeAsync(humanoidRootPart.Position, destination.Position )
	return path
end

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

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

local function checkIfStuck(destination)
	local path = getPath(destination)
	
	local orgin = figure:WaitForChild("HumanoidRootPart")
	local direction = (figure.HumanoidRootPart.CFrame.LookVector).unit * 1
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, figure)
	
	if hit:IsA("Part") then
		print("STUCK LOL")
		humanoid:MoveTo(destination.Position - (figure.HumanoidRootPart.CFrame.LookVector / 5))
		humanoid.Jump = true
	end
end

local function walkTo(destination)

	local path = getPath(destination)
	currentDestination.Value = destination
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				humanoid.Jump = true
			end
			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
		print("STUCK LOL")
		humanoid:MoveTo(destination.Position - (figure.HumanoidRootPart.CFrame.LookVector / 5))
		humanoid.Jump = true
	end
end

local function Patrol()
	local randomNum = math.random(1,#waypoints)
	walkTo(waypoints[randomNum])
end


while wait(0.25) do
	Patrol()
end

1 comment

0

I figured I might as well add the entire script so here you go.