How do i make a global player data table?

I want to make a global table (a literal table, not _G or shared) accessible by server scripts only, I tried using a ModuleScript but they apparently only update the global table once before returning the same thing over and over again, Is BindableFunctions the next best method for doing so?

Modulescripts are supposed to return the same thing, so you can access the same exact global shared table for all server scripts

If you have a modulescript just returns a table then it has the same behavior as _G or shared

I meant module scripts always return the first version of the table they returned, so the new players’ data never get onto the table

Do you have a function that makes the module update the values inside of it? It would probably be a bit easier to provide us some code, no?

Yeah i do, This is what i wrote in the module scirpt

local data = {}
local template = require(game.ServerScriptService.DataTemplate)

function data:Add(player, profile)
	if not player or not profile then return end
	data[player.UserId] = profile
end

function data:Set(player, key, value)
	if not player or not key then return end
	if not data[player.UserId] then return end
	if not data[player.UserId][key] then return end
	data[player.UserId][key] = value
	game.ServerScriptService.DataHandler.Updated:Fire(player, key, value)
end

setmetatable(data, {
	__index = function(self, key)
		if not tonumber(key) then return end
		if not game.Players:GetPlayerByUserId(tonumber(key)) then return end
		return data[key] or template
	end,
})

return data

they dont actually return a table, the module script will always return a reference to the same table in memory

so if you require from many scripts and change it from one, then those changes will be the same for every script that sees the table

the only thing is that the code in the module script doesn’t get rerun every time you require it

The thing modules return is global.
example:
ModuleScript:

local module = {
	value = 0;
}

return module

ServerScript0:

local m = require(game.ReplicatedStorage.ModuleScript)

while true do
	wait(1)
	m.value += 1
	print(script, m.value)
end

ServerScript1:

local m = require(game.ReplicatedStorage.ModuleScript)

while true do
	wait(1)
	print(script, m.value)
end

So only first script changes the value. Second script prints updated value.

Does it even set anything with this condition
if not data[player.UserId][key] then return end
?..

But the changes only appear when i add to the table, so whenever i use require() it just returns the table with these 2 functions and nothing else, But i saw from this the modulescript documentation:

A ModuleScript is a script type that returns exactly one value by a call to require(). ModuleScripts run once and only once per Luau environment and return the exact same value for subsequent calls to require().

It does set something with that condition, Because the script only adds new items to the table using the data:Add function, and then the data:Set function is used to update whatever info data:Add added, and data:Add is called right after the player’s data loads from the datastore, but after that happens, whenever i print the output of require(DataModule) afterwards using run command, it returns {[“Add”] = function, [“Set”] = function}

tested this:

local data = {}
local template = {}

function data:Add(player, profile)
	if not player or not profile then return end
	data[player.UserId] = profile
end

function data:Set(player, key, value)
	if not player or not key then return end
	if not data[player.UserId] then return end
	if not data[player.UserId][key] then return end
	data[player.UserId][key] = value
	game.ServerScriptService.DataHandler.Updated:Fire(player, key, value)
end

setmetatable(data, {
	__index = function(self, key)
		if not tonumber(key) then return end
		if not game.Players:GetPlayerByUserId(tonumber(key)) then return end
		return data[key] or template
	end,
})

return data

with

--!strict

local m = require(game.ReplicatedStorage.ModuleScript)

local players = game.Players

players.PlayerAdded:Connect(function(player: Player)
	m:Add(player, { my_data = "profile"; })
end)

while true do
	task.wait(1)
	local m = require(game.ReplicatedStorage.ModuleScript)
	print(m)
end

works for me (Server & 2 clients).

â–Ľ table: 0x6c64afb96f2be243 = {
    [-2] =  â–Ľ table: 0xed495633f6a33d43 = {
       ["my_data"] = "profile"
    },
    [-1] =  â–Ľ table: 0x82052a82b9e24a13 = {
       ["my_data"] = "profile"
    },
    ["Add"] = "function",
    ["Set"] = "function"
 }  -  Server

the command bar has a different environment from the server and client
you have to print it from a script

game.ServerScriptService.DataHandler.Updated:Fire(player, key, value)

Are you using a BindableEvent to pass a table?

BindableEvent clone table every time it fired. It’s better to use a signal module like GoodSignal.

Yeah i tested it on my game and it worked for me too, I apologize it must be how i require the data

Sorry im not familiar with signal modules, Ill look into it