Issue regarding constantly referencing a table with player data

Currently I’m coding a game which relies heavily on saved user data ( which doesn’t? ) but have ran into an issue that I would like to future proof against, basically for all my module functions I pass through something called “DataTable” which is basically a reference to a table containing all the users data i.e

local DataTable = {[USERID] = {data here}, [USERID] = {data here}}

--------------------------

function ModuleScript.DoSomething(player : Player, datatable : {[number] : any})
local UserID = player.UserId

-- do something, i.e use the userid on the datatable to reference data.
end

The issue is, I would like to be able to call some of the functions in these modules from any script, however I’d need to have a reference to the table which is in a script containing data-saving functions.

Is there a better way to handle / store temporary ( " Live " ) user data so that any script can reference it? these functions are designed to take in the value from said table and modify it accordingly so it’s important I can access this data. I’ve considered maybe just using a folder with values but that feels primitive and like a step back.

I looked around and saw some open source modules which may be able to solve these issues, but I have no experiencing using these and am unsure how up to date these are.

If you’re looking for something like a global table, Roblox has a special one called _G that’s visible to all scripts in the same context (i.e., you can have a server _G and a client _G).

You can put anything in it, it functions just like any other table

I appreciate the response, I will research _G a bit and get back to you :slight_smile:

Placing the data into a separate module script should do the job fine.

-- On the server:
local playerDataMap = {}

local PlayerData = {}

function PlayerData.Add(userId, dataTable)
	playerDataMap[userId] = dataTable
end

function PlayerData.Retrieve(token)
	if typeof(token) == "Instance" and token:IsA("Player") then
		return playerDataMap[token.UserId]
	end
	
	return playerDataMap[token]
end

-- Should run in PlayerRemoving
function PlayerData.Remove(userId)
	playerDataMap[userId] = nil
end

return PlayerData

If you want the data to replicate live to the client, you’ll need to create a system similar to ReplicaService.

Well it’s more I have a main script that has all the data saving functions & the actual table storing it, I need to be able to access this table so I can modify the values in it. I am using module scripts to store my functions so it’s necessary to pass the storage table to the module function so that it can access it.

I was wondering if theres a way the module script can simply access that table without needing to constantly be passed the table as a value. _G looks somewhat promising but a lot of posts are suggesting against it.

If I understand you correctly, you want to get rid of the second parameter.

Then, using the code I provided:

local PlayerData = require(PATH_TO_PLAYER_DATA)

function Module.foo(player)
    local dataTable = PlayerData.Retrieve(player)
    -- everything else
end

Any modifications you make to dataTable will be applied for any other script reading it because it’s a reference to the exact table.

Okay that makes sense, I will look to implement this :smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.