I've decided to learn AI Pathfinding, very cool. It works at first, after a couple of days I did some changes, those changes made the Waypoints nil.

I've tryed setting the Start and End block to CanCollide Off, and even setting them to a CollisionGroup.

Here's the code(server script):

local pathFindingService = game:GetService("PathfindingService")

local activeEnemies = workspace.ActiveEnemies

local Assets = workspace:WaitForChild("Assets")

local mapFolder = Assets:WaitForChild("Map")
local pathModel = mapFolder:WaitForChild("Path")
local pathFindingPoints = pathModel:WaitForChild("PathFindingPoints")

local start = pathFindingPoints:WaitForChild("StartPoint")
local End = pathFindingPoints:WaitForChild("EndPoint")

activeEnemies.ChildAdded:Connect(function(object)
	if object:FindFirstChild("Humanoid") and object:FindFirstChild("Head") then
		if object["Humanoid"].Health > 0 then
			if object:FindFirstChild("Enemy") then

				local humanoid = object["Humanoid"]

				local path = pathFindingService:CreatePath()
				
				local wayPoints
				
				if start.Position and End.Position ~= nil then 
					path:ComputeAsync(start.Position, End.Position)
					
					wayPoints = path:GetWaypoints()
				else
					warn("Start.Position or End.Position is nil")
				end

				if wayPoints[1] ~= nil then

					local walkAnim = object:WaitForChild("AnimScript").Walk
					local walkTrack = humanoid:LoadAnimation(walkAnim)

					if walkTrack.IsPlaying == false then
						walkTrack:Play()
					end

					if wayPoints[math.random(1,#wayPoints)] ~= nil then

						for i, wayPoint in pairs(wayPoints) do
							if (i == 1) then
								object:MoveTo(start.Position)
								print("Moved to start.Position!")
							end
							humanoid:MoveTo(wayPoint.Position)

							humanoid.MoveToFinished:Wait()
						end
						object:Destroy() -- destroys the rig after the pathfinding for loop ends

					end
				end
			end
		end
	end
end)

As always, have a nice day!

1 comment

0

Hm, already 2 hours no answer. Guess you leave me no choice but to figure it out myself!