Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to set up big tables?

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.

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

2 comments

0

make sure to use setmetatable when making metatables.

0

So do you think using metatables is the way to do this? Should line 20 just say setmetatable(UserId_list[key], rss_meetatable)

1 answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.