Set data of a table once and access it from local scripts

Hi
I want to make it so that the server script once per server generates a table basically just puts data into a table and its stored in a way that local scripts can access it. I tried having the server script call a function inside a module to generate that table stored in this module but when players access it its empty.

So you need to know that script enviroments are not shared between computers.

lets say I have a module in replicated storage

module = {}
module.mytable = {}
return module

then I have this script in server storage:

m = require(game.ReplicatedStorage.Module)
m.mytable["test"] = 5 --now anywhere on the SERVER will see this change

on the client lets say I had this

wait(10)
m = require(game.ReplicatedStorage.Module)
print(m.mytable["test"]) --prints nil because you are on a seperate device

if you only generate the table once and don’t make any changes, you can send the table to the client using a remote function.

if it’s a mixed table with numbers and strings as keys you can use a serializer like

function module.tableSerialize(myTable)
	local tempTable = {}
	local i =1
	for k,v in pairs(myTable) do
		if type(v)=="table" then
			v=module.tableSerialize(v)
		end
		tempTable[i] = {k,v}
		i+=1
	end
	return tempTable
end


function module.tableDeserialize(myTable)
	local tempTable = {}
	for _,t in pairs(myTable) do
		if type(t[2]) == "table" then
			t[2] = module.tableDeserialize(t[2])
		end
		tempTable[t[1]] = t[2]
	end
	return tempTable
end
1 Like