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
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.
local number = --the number in the table number = number + change
...Let me know what you think.
What I'm doing at the moment is:
local _M = {} local UserId_list = {} -- a list of "player_" .. player.UserId function _M.player_info(player) -- to be called when looking for the info table local key = tostring("player_" .. player.UserId) if UserId_list[key] then -- if there is already an entry in the table, return it return UserId_list[key] --returns the table to be manipulated by another script and then it'll be sent back and stored again else local rss_meetatable = { ["gems"] = 0, ["gold"] = 0, ["food"] = 0, ["stone"] = 0, ["wood"] = 0, ["premiums"] = 0 } --a dictionary of resources and their starting value UserId_list[key] = rss_meetatable -- creates a new key with the value set to the table above return UserId_list[key] --returns the table to be manipulated by another script and then it'll be sent back and stored again end end return _M
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 :)
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])
local dataStore = game:GetService("DataStoreService") local data = {} local plr game.Players.PlayerAdded:Connect(function(player) plr = player local key = "player_".. plr.UserId if dataStore:GetAsync(key,data[plr]) == nil then data[plr] = {[ ["gems"] = 0, ["gold"] = 0, ["food"] = 0,} else dataStore:GetAsync(key,data[plr]) end end)