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.)