Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to set up big tables?

Asked by 5 years ago

Hi guys,

I'd just like your advice of where to go with this, I think I'm on the right track but im not sure how to execute it... (I think I might be a bit out of my depth haha)

So I want to make a table which i can call from anywhere (so i'm putting it in a module script) which stores all of the values associated with a player. These will include currencies, experience data etc. I think I want to structure this as follows: (let me know if you'd do it differently

  1. Table where: index = "player_" ... player_UserId --(this will let me find the correct thing to edit late?) value = another table (see below) 2 (the value from part 1). Table where: index = file names (e.g. resources or data) value = another table (see below) 3 (the value from part 2, inside of ["resources"]). Table where: (theres another section similar to 2 to organise information, or...) index = currencies (e.g. gold, wood, stone etc.) value = the value associated with the index (this is the number to do maths on etc

I'm not sure if metatables are what I need because the table will just be storing numbers. Obviously these numbers will be changed as the player advances however, this addition/subtraction could just be done by getting the value out of the table e.g.

1local number = --the number in the table
2number = number + change

...Let me know what you think.

What I'm doing at the moment is:

01local _M = {}
02 
03    local UserId_list = {} -- a list of "player_" .. player.UserId
04 
05    function _M.player_info(player) -- to be called when looking for the info table
06        local key = tostring("player_" .. player.UserId)
07 
08            if UserId_list[key] then -- if there is already an entry in the table, return it
09            return UserId_list[key] --returns the table to be manipulated by another script and then it'll be sent back and stored again
10 
11        else
12            local rss_meetatable = {
13                ["gems"] = 0,
14                ["gold"] = 0,
15                ["food"] = 0,
View all 26 lines...

Is this script even close to doing what I'm tying to achieve? Is there a better way to do this?

Hopefully what I'm trying to do/explain has made sense and you can give me some pointers for what to read or what to do differently. Thank you!

If you have any questions please comment :)

0
make sure to use setmetatable when making metatables. JesseSong 3916 — 5y
0
So do you think using metatables is the way to do this? Should line 20 just say setmetatable(UserId_list[key], rss_meetatable) AlexTheCreator 461 — 5y

1 answer

Log in to vote
0
Answered by
FirezDevv 162
5 years ago
Edited 5 years ago

Hey so the best way I Find to do it in a server script this will create a table of stuff the plr has and to access it its just as saying data[plr] to access something like the gems you can do data[plr]["Gems] and it will return the amount of gems and to set the async just do dataStore:SetAsync(key,data[plr])

01local dataStore = game:GetService("DataStoreService")
02local data = {}
03local plr
04game.Players.PlayerAdded:Connect(function(player)
05plr = player
06  local key = "player_".. plr.UserId
07if dataStore:GetAsync(key,data[plr]) == nil then
08   data[plr] = {[ ["gems"] = 0, ["gold"] = 0,   ["food"] = 0,}
09else
10 dataStore:GetAsync(key,data[plr])
11 
12   end
13end)
Ad

Answer this question