Which way is better for updating this client data table? Does it matter?

I recently tried to do client data that is stored inside a script, such as a LocalScript. And was wondering what would be an optimized way to change the data.

Here are two examples that I have.

Example 1

LocalScript:

local testModule = require(game.ReplicatedStorage.ModuleScript)
local clientData = {}

local returnedData = testModule.doStuff("example", clientData)

-- clientData["example"] = returnedData
-- If you would do this, it would replace that what has been done in module.otherStuff()

for k,v in pairs(returnedData) do
	clientData["example"][k] = v
end

print(clientData)

 

ModuleScript

local module = {}

function module.otherStuff(example, clientData)
	if not (clientData[example]) then
		clientData[example] = {}
	end
	
	clientData[example].otherTest = "another value"
end

function module.doStuff(example, clientData)
	local data = {}
	data.test = "Value"
	data.test2 = "Value2"
	data.test3 = "Value3"
	module.otherStuff(example, clientData)
	data.test4 = "Value4"
	
	return data
end

return module

example_1.rbxl (33.3 KB)

 

Example 2

LocalScript

local testModule = require(game.ReplicatedStorage.ModuleScript)
local clientData = {}

testModule.doStuff("example", clientData)

print(clientData)

 

ModuleScript

local module = {}

function module.otherStuff(example, clientData)
	if not (clientData[example]) then
		clientData[example] = {}
	end
	
	clientData[example].otherTest = "another value"
end

function module.doStuff(example, clientData)
	if not (clientData[example]) then
		clientData[example] = {}
	end
	
	clientData[example].test = "Value"
	clientData[example].test2 = "Value2"
	clientData[example].test3 = "Value3"
	module.otherStuff(example, clientData)
	clientData[example].test4 = "Value4"
end

return module

example_2.rbxl (33.1 KB)

 

I made both examples as a downloadable file, so that others can try out better.

Basically, both do the same things, but in different ways. The reason why these functions are there, are just there for an example.

What I am wondering, which way is more “optimized” or if it doesn’t matter at all.

1 Like

It’s up to you. Performance impact is so minuscule that worrying about it is just a waste of time. Do what is most comfortable for you. In this case, it’s production over performance.

So, it doesn’t matter (within reason).

2 Likes

I mean u could have a requestdata option

– the localscript
local Module = require(game.ReplicatedStorage.Module);

local Cash = Module.RequestData(Player,“Cash”)
– on the module
local module = {}

function module.RequestData(Player,DataNeeded)
return 500 – howrver u get ur daya
end

return module