What's the best way to store data in a object?

I want to be able to store data into a part so that it can be easily accessible.

Right now I’m using StringValues, IntValues, and ObjectValues to do this. But it just doesn’t seem right. And I’ve already tried searching the devforum.

1 Like

Have you tried using DataStore or Modules? What are you storing data for?

Ok, I’ll try using ModuleScripts.

2 Likes

Instead of value objects you can just use a table. If you need to keep track of data for multiple instances you can use a nested table with the instances as the keys. And if this is done in a module script the data can be shared across multiple scripts, essentially giving the same effect as putting value instances inside the object.

-- module script
return {}
-- script A
local data = require(path_to_modulescript)
data[workspace.SomePart] = {
	value1 = "hello",
	value2 = 5,
	value3 = game.Workspace
}
-- script B
wait(5) -- let script A update the table first
local data = require(path_to_modulescript)
-- the first result of a require() call is cached, so 'data' here
-- is the same exact table that was modified by script A.
for inst, subtable in pairs(data) do
	print("object: "..inst:GetFullName())
	for key, value in pairs(subtable) do
		print("\t"..key..": "..tostring(value))
	end
end
1 Like