Module can't find part

Hi there!

I’m trying to make a robbery system. Right now, my goal is to program a system that prints out a table of players touching the part at the time of the robbery. This seems like a good starting point to me, but a silly error isn’t allowing this system to work:

LINE 23 OF THE MODULE: attempt to index field ‘bank’ (a nil value). Here is line 23:

local touchingParts = self.bank:GetTouchingParts()

I feel like this issue is related to how I’m doing OOP but I’m not sure.

Here is the full module so far:


local robbing = {}
robbing.__index = robbing

local players = game:GetService("Players")

function robbing:new(part)
	local self = setmetatable({}, robbing)
	
	self.bank = part
	self.players = {}
	self.beingRobbed = false
	
	return self
end

function robbing:init()
	if self.beingRobbed then return end
	
	self.beingRobbed = true
	
	local newTime = tick()
	
	local touchingParts = self.bank:GetTouchingParts()
	
	for i = 0, 3, 1 do
		
		wait(1)
		i = i + 1
		
		for i, v in pairs(touchingParts) do
		if (players:GetPlayerFromCharacter(v.Parent)) and not self.players[players:GetPlayerFromCharacter(v.Parent)] then
			table.insert(players:GetPlayerFromCharacter(v.Parent), self.players)
			print(players:GetPlayerFromCharacter(v.Parent))
		end
		end
		
		
		
	end
	
	print("Finished.")
	
	for i, v in pairs(players) do print (v) end
end

return robbing

Here is the server script that I’m only using to test right now. The “workspace.Bank” argument is just a simple part in workspace.


local robModule = require(script.Rob)

wait(1)

robModule:new(workspace.Bank)

wait(1)

robModule:init()

I’m also taking advice on how to make this system more efficient / use better code practices. I want to make this system work very efficiently. Thanks!

Yes, you are correct with your OOP structure being incorrect but at least its an easy fix. I prefer not to use function:blah() but thats just me.
Your creating a variable called self? this isn’t right
This is what I would do:

local robbing = { }
local Players = game:GetService("Players")

robbing.new = function(self, part)
    self.bank = part
    self.players = { }
    self.beingRobbed = false

    return self
end

robbing.init = function(self)
    if (self.beingRobbed) then return false end

    self.beingRobbed = true

    local newTime = tick()
	
    local touchingParts = self.bank:GetTouchingParts()
	
	for i = 0, 3, 1 do
		wait(1)
		i = i + 1
		
		for i, v in pairs(touchingParts) do
		    if (players:GetPlayerFromCharacter(v.Parent)) and not self.players[players:GetPlayerFromCharacter(v.Parent)] then
			table.insert(players:GetPlayerFromCharacter(v.Parent), self.players)
			print(players:GetPlayerFromCharacter(v.Parent))
		    end
		end
	end
	
	print("Finished.")
	
	for i, v in pairs(players) do print (v) end
end

return robbing

Note: I don’t really know how you had this setup so this is my stab at what you meant to do.

You call this method which just creates the table but don’t capture what it returns.

The Rob module by default has no field for ‘bank’

so this should error under init()

Perhaps you meant to call init on the table return by :new()?

local robModule = require(script.Rob)
wait(1)
local rob = robModule:new(workspace.Bank)
wait(1)
rob:init()
1 Like

This shows that I should be going to sleep soon. Thank you very much! One question though, would you have any idea on how to fix my loop? I’m trying to add players touching the part, but only for players that have been on the part either when the robbery started or less than 3 seconds after it starts, after that, no more players can be added. It sounds very simple, but I don’t even know where to get started.

@froghopperjacob Thanks! I’m going to be changing my OOP structure. Although, I thought using a colon essentially “creates” self for you, so you don’t have to do function(self). What’s the difference in your code and mine?

Thanks for the help :).

Try waiting for the Bank part in your new call:

local rob = robModule:new(workspace:WaitForChild("Bank"))

I didn’t read the post fully, my bad, but if this causes the WaitForChild to wait forever, it probably means you are waiting for a part that doesn’t have the name Bank in the first place.

Sorry really never used that method of self :man_shrugging: but your overriding self with a metatable which really doesn’t make sense. Also the top metatable doesn’t seem to have a purpose. If worst comes to worst you don’t have to use self and just create a local variable