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
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