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