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

Pathfinding to AI?

I have created a basic zombie off a tutorial but I need to redo movement into pathinding so that it does not like get stuck in front of objects so it can make it's way round the map to find the player and kill him for soul purpose of the game. However I don't know how to do this as it is very tricky. Please help!

Here is the current script:

torso = script.Parent.Torso
range = 50
armDmg=5
torDmg=10
target = nil

function findNearestTarget()
	plrs = game.Players:GetChildren()
	for i,plr in ipairs (plrs) do
		if plr.Character ~= nil then
			if plr.Character:findFirstChild("Humanoid") and plr.Character.Humanoid.Health>0 then
		   tor = plr.Character.Torso
			if target ~= nil then
			 if(torso.Position-tor.Position).magnitude < (torso.Position-target.Torso.Position).magnitude then
				print(plr.Name.." is in range")
				target = plr.Character
				break
			end
		elseif (torso.Position-tor.Position).magnitude <= range then
				print(plr.Name.." is in range")
				target = plr.Character
				break
		        end
		    end
	    end
    end
end
	
function hitArm(hit)
	if hit.Parent ~= nil and hit ~=nil then
	    if hit.Parent:findFirstChild("Humanoid") then
		    hit.Parent.Humanoid:TakeDamage(armDmg)
	    end
	end
end
function hitTorso (hit)
if hit.Parent ~= nil and hit ~=nil then
	    if hit.Parent:findFirstChild("Humanoid") then
		    hit.Parent.Humanoid:TakeDamage(torDmg)
	    end
	end
end


script.Parent["Left Arm"].Touched:connect(hitArm)
script.Parent["Right Arm"].Touched:connect(hitArm)
script.Parent["Torso"].Touched:connect(hitTorso)	
	
while true do
	wait (.1)
	findNearestTarget()
	if target ~= nil then
		script.Parent.Zombie:MoveTo(target.Torso.Position,target.Torso)
	end
end
	

1 answer

First time using path finding service, so this may well need debugging.

torso = script.Parent.Torso
range = 50
armDmg=5
torDmg=10
target = nil

function findNearestTarget()
    plrs = game.Players:GetChildren()
    for i,plr in ipairs (plrs) do
        if plr.Character ~= nil then
            if plr.Character:findFirstChild("Humanoid") and plr.Character.Humanoid.Health>0 then
           tor = plr.Character.Torso
            if target ~= nil then
             if(torso.Position-tor.Position).magnitude < (torso.Position-target.Torso.Position).magnitude then
                print(plr.Name.." is in range")
                target = plr.Character
                break
            end
        elseif (torso.Position-tor.Position).magnitude <= range then
                print(plr.Name.." is in range")
                target = plr.Character
                break
                end
            end
        end
    end
end

function hitArm(hit)
    if hit.Parent ~= nil and hit ~=nil then
        if hit.Parent:findFirstChild("Humanoid") then
            hit.Parent.Humanoid:TakeDamage(armDmg)
        end
    end
end
function hitTorso (hit)
if hit.Parent ~= nil and hit ~=nil then
        if hit.Parent:findFirstChild("Humanoid") then
            hit.Parent.Humanoid:TakeDamage(torDmg)
        end
    end
end


script.Parent["Left Arm"].Touched:connect(hitArm)
script.Parent["Right Arm"].Touched:connect(hitArm)
script.Parent["Torso"].Touched:connect(hitTorso)    

while true do
    wait (.1)
    findNearestTarget()
    if target then
	local path = game.Services:GetService("PathFinding"):ComputeRawPathAsync(script.Parent.Zombie.Torso.Position, target.Position)
	local points = path:GetPointsCoordinates()
	for i,v in pairs(points)do
		script.Parent.Zombie.Humanoid:MoveTo(v.Position)
	end
	
    end
end


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