Attempt to index nil with 'GetChildren' issue

What do you want to achieve?
Make the AI walk to random waypoints.

What is the issue?
The issue is the output keep giving the " What do you want to achieve?
You know the “creator” thing to know who it who that killed the person in Classic Sword,I’m putting it on my sword but I’m having an error with a function related to the above…

What is the issue?
The problem is that it tells me attempt to index nil with ‘GetChildren’.
It said that the issue is in this line:

function Bot:patrol()
	local waypoints = self.killerWaypoints:GetChildren()
	local randomPoint = math.random(1, #waypoints)
	Bot:moveTo(waypoints[randomPoint])
end

This is the full script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local PhysicsService = game:GetService("PhysicsService")
local PathfindingService = game:GetService("PathfindingService")
local Debris = game:GetService("Debris")

local Bot = {}
Bot.__index = Bot

function Bot:new(model)
	local killer = setmetatable({},Bot)	
	-- childs
	killer.model = model
	killer.humanoid = model:WaitForChild("Humanoid")
	killer.rootPart = model:WaitForChild("HumanoidRootPart")
	-- configs
	killer.sounds = ReplicatedStorage:WaitForChild("Sounds")
	killer.animations = ReplicatedStorage:WaitForChild("Animations")
	killer.name = model.Name
	killer.killerWaypoints = model.Parent:WaitForChild("KillerWaypoints")
	
	killer.humanoid.WalkSpeed = 15.5
	
	Bot:setPhysics()
	Bot:animate()
	
	local firstPoint = killer.killerWaypoints:FindFirstChild("First")
	if not firstPoint then
		firstPoint = killer.rootPart.Position + Vector3.new(0,0,10)
	end
	killer.humanoid:MoveTo(firstPoint.Position)
	killer.humanoid.MoveToFinished:Wait()
end

function Bot:setPhysics()
	local success, err = pcall(function()
		self.model.PrimaryPart:SetNetworkOwner(nil)
	end)
	
	if not success then
		warn("Killer:", err)
	end
end

function Bot:animate()
	
end

function Bot:getPath(destination)
	local pathParams = {
		["AgentHeight"] = 4,
		["AgentRadius"] = 7,
		["AgentCanJump"] = false
	}
	
	local path = PathfindingService:CreatePath(pathParams)
	path:ComputeAsync(self.rootPart.Position - destination.Position)
	
	return path
end

function Bot:canSeeTarget(target)
	local origin = self.rootPart.Position
	local direction = (target.HumanoidRootPart.Positon - origin).unit * 80
	
	local rayParams = RaycastParams.new()
	rayParams.FilterDescendantsInstances = {self.model, self.model.Parent:FindFirstChild("ClickDoors")}
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local hit = workspace:Raycast(origin, direction, rayParams)
	
	if hit then
		if hit:IsDescendantOf(target) then
			return true
		end
	else
		return false
	end
end

function Bot:findTarget()
	local players = game.Players:GetPlayers()
	local nearestTarget = nil
	local maxDistance = 50
	
	for index, player in pairs(players) do
		local character = player.Character or player.CharacterAdded:Wait()
		if character then
			local target = character
			local distance = (self.rootPart.Position - target.HumanoidRootPart.Position).magnitude
			if distance < maxDistance and Bot:canSeeTarget(target) then
				maxDistance = distance
				nearestTarget = target
			end
		end
	end
	
	return nearestTarget
end

function Bot:chaseTarget(target)
	local distance = (target.HumanoidRootPart.Position - self.rootPart.Position).magnitude
	if distance > 2 then
		self.humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		print("Kill")
		target.Humanoid.Health = 0
	end
end

local function displayPath(waypoints)
	local color = BrickColor.Random()
	for index, waypoint in pairs(waypoints) do
		local part = Instance.new("Part")
		part.BrickColor = color
		part.Anchored = true
		part.CanCollide = false
		part.Size = Vector3.new(1,1,1)
		part.Position = waypoint.Position
		part.Parent = workspace
		local Debris = game:GetService("Debris")
		Debris:AddItem(part, 6)
	end
end

function Bot:moveTo(destination)
	local path = Bot:getPath(destination)
	
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = Bot:findTarget()
			if target and target.Humanoid.Health > 0 then
				Bot:chaseTarget(target)
				break
			else
				displayPath(path:GetWaypoints())
				self.humanoid:MoveTo(waypoint.Position)
				self.humanoid.MoveToFinished:Wait()
			end
		end
	else
		self.humanoid:MoveTo(destination.Position - (self.rootPart.CFrame.LookVector * 10))
	end
	
end

function Bot:patrol()
	local waypoints = self.killerWaypoints:GetChildren()
	local randomPoint = math.random(1, #waypoints)
	Bot:moveTo(waypoints[randomPoint])
end

return Bot

Other script that seems to be needed:

local CollectionService = game:GetService("CollectionService")

local Bot = require(script.Bot)
local Players =require(script.Player)

CollectionService:GetInstanceAddedSignal("KillerBot"):Connect(function(model)
	Bot:new(model)
	while wait(1) do
		Bot:patrol()
	end
end)

CollectionService:GetInstanceAddedSignal("KillerPlayer"):Connect(function(model)
	
end)

local bot = script.Dummy:Clone()
bot.Parent = workspace.Map
if bot then
	CollectionService:AddTag(bot, "KillerBot")
	bot:SetPrimaryPartCFrame(workspace.Map.KillerSpawn.CFrame)
end

What solutions have you tried so far?
I tried to read the script again and again to find where is the error come from but still blank and idk what to do with it :confused: pls help

1 Like

So it is not finding the waypoints it expects. These are determined under Bot:new in line

killer.killerWaypoints = model.Parent:WaitForChild("KillerWaypoints")

Do these exist?

It does exist
image

That is not what the script is looking for:

function Bot:new(model)  <-- model
	local killer = setmetatable({},Bot)	
	-- childs
	killer.model = model   <-- model
-- more stuff, then
	killer.killerWaypoints = model.Parent:WaitForChild("KillerWaypoints")  <<-- model Parent "Workspace"

It expects to find the waypoints defined under the model Parent Try:

	killer.killerWaypoints = model.Parent.Map:WaitForChild("KillerWaypoints")

new issue:Map is not a valid member of Folder "Workspace.Map"

Do you Parent the NPCs to the Workspace or to a folder under Workspace?

I parented the npc to the map folder

Okay, so my advice was bad then :frowning: and your original was correct. Any other errors? It’s late here, so I don’t wanna spin up Studio to test

idk but it says the error is here:

function Bot:patrol()
	local waypoints = self.killerWaypoints:GetChildren()
	local randomPoint = math.random(1, #waypoints)
	Bot:moveTo(waypoints[randomPoint])
end

That is looking for the parts that act as the waypoints in Workspace > Map > KIllerWayPoints, but it is not finding any there. Are the parts present and Anchored?

image

OK. I will spin up a copy in the morning and test if nobody else chimes in to the convo.

OK. I have created a basic clone of your script and have replicated the error. I have a look at it today to work out what is wrong

I personally have never messed with setmetatables or any of that area
But obviously it’s saying that

self.killerWaypoints == nil

So looking at your script, again maybe im wrong
is when inside the Bot:new
when you do the setmetatable({},Bot)
anything you put inside/localize for killer doesnt go to the Bot

Maybe try reversing the setmetatable?

setmetatable(Bot,{})

again, i havent messed with stuff like this before. what you could do is inside Bot:patrol
do prints of the other stuff you have assigned to killer

function Bot:patrol()
     print(self.model)
     print(self.humanoid)
end

Obviously if thos come back not nil then I could just be wrong, but if they do come back nil, maybe im right??

The setmetatable() global returns the table which is having its metatable set (the first table argument to the function).

local killer = setmetatable({}, Bot)

Is the same as:

local killer = {}
setmetatable(killer, Bot)

In an OOP-like manner, they want the object which is represented by the table variable named “killer” to inherit from the class-like structure named “Bot”.