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.