Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would you correctly nest this table?

Does anyone know of a better way to nest the 'this' table while still being able to use it as a dictionary? It looks ugly and I'm blanking.

local bank = {}
bank.__index = bank

setmetatable(bank, {
	__index = function(ogt, bank_id)
		-- Converting to a dictionary and nesting 'this'
		ogt[bank_id] = {}
		local this = ogt[bank_id]
		this.balance = 0
		this.banker = bank_id
		setmetatable(this, bank)
		return this
	end
})

function bank:withdraw(how_much)
	self.balance = self.balance - how_much
	return self.balance
end

function bank:deposit(how_much)
	self.balance = self.balance + how_much
	return self.balance
end

function bank:get_balance()
	return self.balance
end

function bank:set_balance(how_much)
	self.balance = how_much
	return self.balance
end

print(bank['person299']:set_balance(10))

`

1 answer

ogt[bank_id] = {balance = 0, banker = bank_id}

1 comment

0

Thanks, but that's not what I mean :3

This discussion is preserved as it was. New answers, comments, and votes are closed.