I cannot figure out this error

Issue:

I have tried everything to fix this. But the I get the error “AirShipDetection:17: attempt to get length of a nil value:”

local AirShipDetection = {}
AirShipDetection.__index = AirShipDetection

function AirShipDetection.new(newTable)
	local self = setmetatable(newTable or {}, AirShipDetection)
	self.airships = {}
	return self
end

function AirShipDetection:AddAirShip(airship)
	table.insert(self.airships, airship)
	print(#self.airships)
end

function AirShipDetection:Update(Character, isPointInVolume)
		
	if #self.airships > 0 then
	for _, airship in ipairs(self.airships) do
			if isPointInVolume(Character.HumanoidRootPart.Position, airship.CFrame, airship.Size) then
			
		end
	end
	
	end

end

return AirShipDetection

The error comes from the #self.airships > 0 line. It works and prints fine just above in the add airship function (which gets called from the client script and adds airship hitboxes when they spawn in) but not in the update function…I have no idea really why this is happening, I did a code test in a new script and it works fine. The code above is the DetectionClass for the airships, I will get you guys the DetectionHandler, and then below that ill put my client script.

CharacterDetectionManager.__index = CharacterDetectionManager

local AirShipModule = require(script.AirShipDetection)

local function isPointInVolume(point: Vector3, volumeCenter: CFrame, volumeSize: Vector3): boolean
	local volumeSpacePoint = volumeCenter:PointToObjectSpace(point)
	local halfSize = volumeSize/2
	return volumeSpacePoint.X >= -halfSize.X
		and volumeSpacePoint.X <= halfSize.X
		and volumeSpacePoint.Y >= -halfSize.Y
		and volumeSpacePoint.Y <= halfSize.Y
		and volumeSpacePoint.Z >= -halfSize.Z
		and volumeSpacePoint.Z <= halfSize.Z
end

function CharacterDetectionManager.new()
	local self = setmetatable({}, CharacterDetectionManager)
	self.detectionSystems = {}
		
	return self
end

function CharacterDetectionManager:AddDetectionSystem(detectionModule)
	table.insert(self.detectionSystems, detectionModule)
end

function CharacterDetectionManager:initialize()
	self:AddDetectionSystem(AirShipModule)
end

function CharacterDetectionManager:Update(Character)
	for _, detection in ipairs(self.detectionSystems) do
		detection:Update(Character, isPointInVolume)
	end
end

return CharacterDetectionManager
--//services//--

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")

--//modules//--

local modules  = {}
local g = ReplicatedStorage.ModulesClient:GetDescendants()

--//Vars//--

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character.HumanoidRootPart

--//Remotes//--

local RemoteEvent = ReplicatedStorage.RemoteEvents.MainRemote

--//ModuleLoading//--

for i = 1, #g do
	if g[i].ClassName == "ModuleScript" then
		
		--//Storing modules in dictionary
		
		modules[g[i].Name] = require(g[i])
		print("Loaded:",g[i])
	end
end

--//MainRemote

RemoteEvent.OnClientEvent:Connect(function(ModuleIdentifier, FunctionIdentifier, ...)
	if not modules[ModuleIdentifier] or not modules[FunctionIdentifier] then
	modules[ModuleIdentifier][FunctionIdentifier](...)
	end
end)

--//RunService, ClientSide detections

local characterdetectionSystem = modules.CharacterDetectionManager.new()
local airshipdetectionSystem = modules.AirShipDetection.new()
characterdetectionSystem:initialize()


--Airship listener + Initial Check

for _, airship in ipairs(CollectionService:GetTagged("ShipHitBox")) do
	airshipdetectionSystem:AddAirShip(airship)
end

CollectionService:GetInstanceAddedSignal("ShipHitBox"):Connect(function(hitbox)
	airshipdetectionSystem:AddAirShip(hitbox)
end)



RunService.Heartbeat:Connect(function(deltatime)
	characterdetectionSystem:Update(Character)
end)

The reason for this error is because you’re passing in a raw version of the AirShipDetection class instead of an instantiated version when you initialize your instance of the CharacterDetectionManager. Specifically these lines

function CharacterDetectionManager:initialize()
	self:AddDetectionSystem(AirShipModule)
end

You’re passing in a required version of AirShipDetection rather than an instantiated version, so self is just going to be the AirShipDetection class itself, hence why self.airships is nil. Since you create an instantiated version of AirShipDetection in your third script, perhaps you meant to pass this through for the initialization?

local airshipdetectionSystem = modules.AirShipDetection.new()
characterdetectionSystem:initialize(airshipdetectionSystem)

Then you’d have to rework the initialize function as such:

function CharacterDetectionManager:initialize(airshipdetectionSystem) --instantiated AirShipDetection version passed in (.new)
	self:AddDetectionSystem(airshipdetectionSystem) 
end

Though depending on your goal (since I can’t really figure out the purpose of these scripts without much context), this might not be the solution. However my reason for why your scripts don’t currently work should be correct.

Thanks! I thought about doing that but i did not think it would fix it for some dumb reason.

1 Like