I was following a tutorial (I'm new to pathfinding) about AI and stuff, so I followed his tutorial. I put together this long script (sorry about it being long)

local myHead = script.Parent.Legs
local myRoot = script.Parent.HumanoidRootPart
local myHuman = script.Parent.Humanoid

local clone = script.Parent:Clone()
local fireRate = 0.5
local home = myRoot.Position
local sightDistance = 50

function getUnstuck()
	myHuman:Move(math.random(-1,1),0,math.random(-1,1))
	myHuman.Jump = true
	wait(.5)
end

function checkDist(target)
	return (myRoot.Position - target.Position).Magnitude
end

function checkSight(target)
	local ray = Ray.new(myHead.Position,(target.Position - myHead.Position).Unit * sightDistance)
	local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
	if hit then
		if hit:IsDescendantOf(target) then
			return true
		end
	end
	return false
end

function findTarget()
	local dist = 100
	local target = nil
	for i,v in pairs(workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("HumanoidRootPart")
		if checkDist(torso) < dist and human.Health > 0 then
			dist = checkDist(torso)
			target = torso
		end
	end
	return target
end

function pathToTarget(target)
	local pathFind = game:GetService("PathfindingService"):CreatePath()
	pathFind:ComputeAsync(myRoot.Position,target.Position)
	if pathFind.Status == Enum.PathStatus.Success then
		local wayPoints = pathFind:GetWaypoints()
		for i,v in ipairs(wayPoints) do
			if v.Action == Enum.PathWaypointAction.Jump then
				myHuman.Jump = true
			end
			myHuman:MoveTo(v.Position)
			spawn(function()
				wait(0.5)
				if myHuman.WalkToPoint.Y > myRoot.Position.Y then
					myHuman.Jump = true
				end
			end)
			local moveSuccess = myHuman.MoveToFinished:Wait()
			if not moveSuccess then
				getUnstuck()
				break
			end
			if (target.Position - wayPoints[#wayPoints].Position).Magnitude > 15 then
				break
			end
			if checkDist(target) < 15 and checkSight(target) then
				break
			end
			if i % 5 == 0 then
				if findTarget()  ~= target then
					break
				end
			end
			if checkSight(target) then
				script.Parent.Face.BrickColor = BrickColor.new("Really red")
				
			else
				script.Parent.Face.BrickColor = BrickColor.new("Institutional white")
			end
		end
	else
		getUnstuck()
	end
end

function main()
	local target = findTarget()
	if target then
		if checkDist(target) > 15 then
			pathToTarget(target)
		elseif checkDist(target) < 5 then
			
		elseif checkDist(target) > 15 and not checkSight(target) then
			pathToTarget(target)
		end
		if checkSight(target) then
			script.Parent.Face.BrickColor = BrickColor.new("Really red")
		else
			script.Parent.Face.BrickColor = BrickColor.new("Institutional white")
		end
	else
		if (myRoot.Position - home).Magnitude > 20 then
			script.Parent.Face.BrickColor = BrickColor.new("Institutional white")
		else
			script.Parent.Face.BrickColor = BrickColor.new("Institutional white")	
		end
		wait(0.5)
	end
	target = nil
end
while wait() do
	if myHuman.Health > 0 then
		main()
	else
		break
	end
end

Why does it index nil with position? What on earth is happening? Please help! (P.S, here's the error)

Workspace.Robot.AI:17: attempt to index nil with 'Position'
Stack Begin
Script 'Workspace.Robot.AI', Line 17 - function checkDist
Script 'Workspace.Robot.AI', Line 37 - function findTarget
Script 'Workspace.Robot.AI', Line 90 - function main
Script 'Workspace.Robot.AI', Line 116
Stack End

2 comments

0

Could you put the error code in the description..

0

K