Previous title: ""Searching For Tools" (GetChildren stuff) in my Pathfinding Script returns nil?" Hello. I'm pretty new to Pathfinding and before I made my script I had to find one on the Toolbox. So in my script I tried to take the challenge, but something's wrong. I put in a print function to see what tool was selected to the nearest, but it prints nil. Am I doing it wrong? If I did, can someone please give me answers? Thanks. Here's the code.

SCRIPT




local hasWep = false

-- IGNORE THE findReload() AND activateAttackFromTool() FUNCTIONS!

function findReload(from,toAmmo,magsF)
	if mags.noOfMags > 1 then
		mags.noOfMags = mags.noOfMags - 1
		currentammo = mags.bulletEachMag
	else
		modulereq:Travel(false)
	end
end

function activateAttackFromTool(isRanged,magsFF,notAGun,rangedDam,meleeDam)
	
end

-- START OF THE EXECUTION OF THE FUNCTION IN THE MODULE

while true do
	wait()
	if hasWep == false then
		modulereq = require(script.Parent.AIModule)
		modulereq:Travel(false)
	else
		
	end
end

MODULE

PF = game:GetService('PathfindingService')

From = script.Parent.HumanoidRootPart

parent = script.Parent

distance = 100

battle_dist = 140



local mod = {}

function mod.Travel(battlemode)
	local folder = workspace.LostToolsFolder:GetChildren()
	local Async = nil
	local goTo = nil
	for i = 1,#folder do
		if folder[i].ClassName == 'Tool' and folder[i]:FindFirstChild('Handle') and folder[i]:FindFirstChild('Compatible') then
			local npcDist = (folder[i].Handle.Position - From.Position).Magnitude
			if battlemode == false then
				if npcDist < distance then
					goTo = folder[i].Handle
				end
			else
				if npcDist < battle_dist then
					goTo = folder[i].Handle
				end
			end
		end
	end
	print(goTo) -- Prints nil
	Async = PF:FindPathAsync(From.Position,goTo.Position)
	local Point = Async:GetWaypoints()
	PointPos = Point[2].Position
	parent.Humanoid:MoveTo(PointPos)
	if PointPos.Action == Enum.PathWaypointAction.Jump then
		parent.Humanoid.Jump = true
	end
end

return mod

2 comments

0

Did you check while in game that the folder "LostToolsFolder" does indeed have those items you are creating the search for? I am assuming the folder contains items, which are typically unanchored. They might simply be falling off the world when the script attempts to run.

0

(I got it fixed) The items are unanchored but has collision, and they do not fall.