Trying to make a bank system with OOP (I think) but one function is not working

So I would like to make a function that when a random bank account is made, it indexes a database using the new bank account’s owner’s name as a key. However, it is not working. It creates the bank account but it doesn’t index into the table using the owner as a key.

This is the output:

This is the script that everything takes place in. It is a child of the workspace:

These are the functions used and the setup of the metatables that create the bank account object. This, of course, is in a module script which is found as a child in serverscriptservice. The function that I am having a problem with is the BankAccount:AddToDatabase() function (I believe).

I am just starting to use metatables and I know that my coding might be kinda funky but I would like to get this solved as soon as possible. Any help is very much appreciated

1 Like

I’m not entirely sure how tables work across functions, but I believe the issue is that the Database variable is being copied, and then the copy gets indexed. The solution would be to have AddToDatabase return the modified database:

-- BankAccount module
function BackAccount.AddToDatabase(self, Database)
    -- yada yada, stuff to do before modifying the database...
    Database[self.Owner] = BankAccount
    return Database
end
-- Main script

for i = 1, 20 do
    -- ...
    TDBankAccountDatabase = newBankAccount.AddToDatabase(newBankAccount, TDBankAccountDatabase)
    -- ..
end

Although I believe the proper fix would be to make a Database type with AddToDatabase function that takes a bank account as an argument, so you could code like this:

TheDatabase.AddAccount(MyAccount)

Instead of this:

TheDatabase = MyAccount.AddToDatabase(MyAccount, TheDatabase)

Also, you should change function signature for the OOP stuff from this:

function MyAccount.DoSomething(self, Thing)
    Thing(self)
end

MyAccount.DoSomething(MyAccount, error)

to this:

function MyAccount:DoSomething(Thing)
    Thing(self)
end

MyAccount:DoSomething(error)

, you not only remove that pesky self you need to include in the function parameters, it also makes for a much cleaner way to call the member functions because self is automatically “inferred” when the function is declared with : instead of . (it assumes self to be the table).

1 Like

Hello phoenix,

Thanks for your advice it is very much appreciated. I have utilized this advice to the fullest and have finally found the answers to my problem. However, I modified the creation of the bank account function by automatically indexing the database by receiving an already made table as an argument and then indexing the database using the randomized ID given to each account created from there. Again, thank you very much. I will mark this as the solution as even though this may be basic, it is good advice for beginners. Thank you.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.