Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I am making a piggy type of game and Ai pathfinding script broken. How can I fix it?

I am making a piggy form of game and my Ai pathfinding script is not working any way I can fix it? Here the script:

local Scientist = script.Parent
local humanoid = Scientist.Humanoid

local PathfindingService = game:GetService ("PathfindingService")

local function getPath(destination)
	local path = PathfindingService:CreatePath()
	
	path:ComputeAsync(Scientist.HumanoidRootPart, destination.Position)
	
	return path
end	

local function walkTo(destination)
	local path = getPath(destination)
	
	for index, waypoint in pairs(path:GetWaypoints()) do
		humanoid:MoveTo(waypoint.Position)
		humanoid.MoveToFinished:Wait()
	end
end

while true do
	walkTo(workspace.destination)
	walkTo(workspace.destination2)
end

1 comment

0

Can you please put everything inside of a lua block? It's really hard to read and fix it this way.

1 answer

Try This

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 100000 -- distance you want away from the player
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end




while true do
	wait(math.random(1,5))
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target)
	end

end

This discussion is preserved as it was. New answers, comments, and votes are closed.