I wish i could be descriptive, I really do, Ask anything and you shall receive. But i have nothing to give. The pathfinding just isnt working. and im getting no errors. Is it punctuation?

FOLLOW CLOSEST PLAYER SCRIPT

local pathfindingservice = game:GetService("PathfindingService")

function FindNearest(part)
	local lowest = math.huge -- infinity
	local NearestPlayer = nil
	for i,v in pairs(game.Players:GetPlayers()) do
		if v and v.Character then
			local distance = (v.Character.HumanoidRootPart.Position-script.Parent:FindFirstChild("HumanoidRootPart").Position).magnitude
			if distance < lowest then
				lowest = distance
				NearestPlayer = v
				return NearestPlayer
			else
				print("Nope!")
			end
			print("Not even close")
		end
		print("Your kidding me right?")
	end
end

local path = pathfindingservice:CreatePath()

while true do
	wait(4)
	local player = FindNearest()
	if not player == nil then
		local humanoidrootpart = player:FindFirstChild("HumanoidRootPart")
		local dummy = script.Parent
		local dummyhumanoid = dummy:FindFirstChild("Humanoid")
		local dummyrootpart = dummy:FindFirstChild("HumanoidRootPart")
		path:ComputeAsync(dummyrootpart.Position, humanoidrootpart.Position)
		dummyhumanoid:MoveTo(humanoidrootpart.Position)
		dummyhumanoid.MoveToFinished:Wait()
		print("Worked")
	end
end

I dont know what to write here. Its supposed to make the dummy follow the closest player. i might add an attack script later but for now im only testing this. Again. Ask for anything and i'll give it in comments. I really wish i could be more descriptive

3 comments

0

Your AI will always follow the first Player, as you immediately return it without actually letting the function hone down. Another thing to note, is how you're returning a Player Object; you proceed to locate a HRP as if it were a Character, it will return nil.

0

Finally, you're not even following the computed path, you simply tell the Dummy Humanoid to go to the last known position of the "HRP". You need to iterate over the Waypoints the ComputAsync function internally produces for the Path Object via Path:GetWaypoints().

0

Even then, you'll still be following the last known Position.