Attempt to index nil with name error

Hi so,
Im currently making my own module for an ai car. I got a version running up fine but then wanted to throw it into a module script and handle them client side. With this i decided to start using self for the first time. When doing this i came across this error, was wondering if anyone could help out :smiley:

Error:

Code:

local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local CarController = {}
CarController.__index = CarController

local iterations = 50
local nextNode

-- New Car Instance
function CarController.new(carModel, frontVector,NodeFolder, StartNode)
  
  assert(typeof(carModel) == 'Instance')
  assert(typeof(NodeFolder) == 'Instance')
  
  local self = setmetatable({
    _CarModel = carModel;
    _FrontVector = frontVector;
    _nodeFolder = NodeFolder;
    _startNode = StartNode;
    _vel = Instance.new("BodyVelocity", frontVector);
  }, CarController)

  
  -- Create Velocitys

  -- Body Velocity
  --self._Vel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
  --self._Vel.P = 500

  -- Body Gyro
  local gyro = Instance.new("BodyGyro", self._FrontVector)
  gyro.P = 5000

  -- Body Force
  local bf = Instance.new("BodyForce")
  bf.Force = self._FrontVector:GetMass() * Vector3.new(0, workspace.Gravity, 0)
  bf.Parent = self._FrontVector

  nextNode = self._startNode
  
  return self
  
end


-- Handles Car
function CarController:Control()
  
  local TweenInfo = TweenInfo.new(1)
  
  RunService.Heartbeat:Connect(function()
    
    -- Where the car is facing
    if ((self._FrontVector.Position - nextNode.Value).Magnitude < 20) then -- Error
      if not nextNode:FindFirstChild("Connector") then
        script.Parent.Parent:Destroy()
        return
      end
      nextNode = nextNode.Connector.Value
      for i = 0, 1 - 1 do
        for j = 0, iterations do
          self._FrontVector.CFrame = self._FrontVector.CFrame:lerp(CFrame.new(self._FrontVector.CFrame.Position, nextNode.Value), (i * iterations + j) / (1 * iterations))  
          wait(1 / iterations)
        end
      end
    end

    self._Vel.Velocity = self._FrontVector.CFrame.lookVector * self._FrontVector.Parent.Speed.Value

    -- Ray
    local ray = Ray.new(self._FrontVector.CFrame.p, self._FrontVector.CFrame.LookVector * 30)

    -- Collision
    local part = workspace:FindPartOnRay(ray)

    -- Stop and Start
    if part then
      TweenService:Create(self._FrontVector.Parent.Speed, TweenInfo, {
        Value = 0
      }):Play()
    else 
      TweenService:Create(self._FrontVector.Parent.Speed, TweenInfo, {
        Value = 50
      }):Play()
    end
  end)
end

return CarController

Can you check if self._FrontVector still exist in workspace? It’s possible that something else deleted it, for example falling into the void

its cloned when the module is called

-- Client
local carModule = require(script:WaitForChild("CarController"))
local env = script.Parent.Parent
local node_folder = workspace.PathNodes

local nextNode = node_folder.StartNode
local carFolder = workspace.CarFolder

wait(.5)

while true do
  wait(3)
  local car = game.ReplicatedStorage.Car:Clone()
  car.Parent = carFolder
  carModule.new(car, car.FrontVector, node_folder, nextNode)
  wait(.1)
  carModule:Control()
end

image

Maybe try this instead

local newCar = carModule.new(car, car.FrontVector, node_folder, nextNode)
wait(.1)
newCar:Control()

worked cheers mate for your help :smiley: