A Database with Metatables

Good morning, I’m trying to do something like a Database with Metatables/OOP.
With this script below we create the NPCs, but i’m wanna to know how to I get his properties.

(Example: When you have a Folder with players data you do something like this)

local SSS = game:GetService("ServerScriptStorage")
local Data = SSS:WaitForChild("Data")
local PlayerData = Data:WaitForChild(plr.Name)
local Life = PlayerData.Life

Life = Life - 10

So I wanna to do something like this, but instead of using Folders + Int/String Values, I wanna use metatables/tables and Variables.

local NpcModule = {}
NpcModule._index = NpcModule

function NpcModule.New(Name, Job, Age)
	local NewNpc = {}
	setmetatable(NewNpc, NpcModule)
	
	NewNpc.Name = Name
	NewNpc.Job = Job
	NewNpc.Age = Age
	
	return NewNpc
end

function NpcModule.GetNpcs()
	return Npc
end

Npc = {}

Names = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "Y", "X", "Z"}
Jobs = {"Farmer", "Blacksmith", "Baker", "Hunter", "Merchant",}

for i = 1, 5 do
   Npc[i] = NpcModule.New(Names[math.random(1, #Names)], Jobs[math.random(1, #Jobs)], math.random(18,116))
end

return NpcModule

And to get Npcs data use:

local module = {}
	local NpcModule =  require(script.Parent:WaitForChild("NpcModule"))
	
	wait(3)
	
	Npc = NpcModule.GetNpcs()
	
	for i = 1, 5 do
	print(Npc[i].Name)
	print(Npc[i].Job)
	print(Npc[i].Age)
	print("----------------")
end
	
return module

(Sorry if sentence is weird, I’m learning english at moment.)

2 Likes

As far as I know, that will work just like if you were using Folders + Int/String Values, there won’t be any problems, it’s your preference about how you create your Database, if you want to make your code neater then you could use metatables/tables and Variables but beginners would use Folders + Int/String Values because it’s just way easier for them to use.

However I have a question, do you have any issues with using metatables/tables and Variables or you just want to know if it’s good to do this or not?

I want to know is how I get Npcs data.

Example:

I have “Npc” Lucas, he is the second npc, I just are trying to get his properties/values/data with a variable after he is created.

Because
print(NpcModule.NewNpc[2].Name)
Isn’t working.

If you want to get the data of them, you could just print their Name, Job, and Age instead of doing print(NpcModule.NewNPC[2].Name):

local NpcModule = {}
	NpcModule.__index = NpcModule
	
	function NpcModule.New(Name, Job, Age)
		local NewNpc = {}
		setmetatable(NewNpc, NpcModule)
		
		NewNpc.Name = Name
		NewNpc.Job = Job
		NewNpc.Age = Age
		
		return NewNpc
	end
	
	NPC1 = NpcModule.New("Herick", "BlackSmith", "34")
	NPC2 = NpcModule.New("Lucas", "Farmer", "42")
	NPC3 = NpcModule.New("John", "Warrior", "22")
	
	print("NPC1 Name: "..NPC1.Name..", Job: "..NPC1.Job..", Age: "..NPC1.Age".")
	print("NPC2 Name: "..NPC2.Name..", Job: "..NPC2.Job..", Age: "..NPC2.Age".")
	print("NPC3 Name: "..NPC3.Name..", Job: "..NPC3.Job..", Age: "..NPC3.Age".")
	
	--
		
return NpcModule

This will print each one’s Name, Job and Age, does that answers your question?

1 Like

Thanks but not.

NPC1 is temporary;
NPC2 is temporary;
NPC3 is temporary.

I will use this in my game.

for I = 1, 5 do
   NPC = NpcModule.New("RandomName", "RandomJob", "RandomAge")
end

But I can’t take 3 npcs localization with only NPC variable.

So I wanna learn how to get NPCs properties without previous Variables like NPC1/NPC2/NPC3.

1 Like

Before I try and come up with a different solution for this problem I just want to point out that these are not variables but they are the values for the tables that you are creating.

OK, so you have 2 options:

  • Option 1 - Get the data values for every NPC that you are creating like I did in my previous solution attempt.

OR

  • Option 2 - You can loop throught the Workspace or whatever the service that you put the NPCs in and then get their data values.
1 Like

I think the same thing. At same way thanks ^^

1 Like