Is serialization the optimal way of saving instances?

I have this game where yo can make a custom card, with custom efffects and tirggers, and i realised well I want to make it so it can save, so then I thought, since there isn’t that much difference between cards i could create a folder with values and have the datastore save the values, and when i want to load them, those specific values are changed, which activates an event in a script in each value that sets the actual property of that card to that value. or would it be easier to just serialize the entire card?

I would say it depends. I think the easiest method would be to save all the data for the player’s card under a single table which contains values that you can use to then change/update the player’s card.

1 Like

Yeah that’s also what i thought. But since I don’t have access to properties this way i would have to set up a folder with values and parent a script to each value that activates when the card’s values change
On that note, do regular scripts also run in replicated first?

I would advise against putting scripts into Replicated. You wouldn’t need to set up a values folder if you use if statements when the player is added. For example:

-- .PlayerAdded function
if Data.CardsData.Value1 == true then

You could probably use a module script that the data updates if you wanted to access values from outside of that script.

1 Like

But how could I reference the specific roperty of a specific instance?
Also I don’t really understand moduleScripts don’t they just hold functions that can be used by any script?

I’m not really sure the exact details since you didn’t provide a script. An example of what you could do is something like this (which updates the player’s inventory from a datastore):

for _,item in pairs(profile.Data.Inventory["Items"]) do
	player.Inventory:FindFirstChild(item.Name).Value = item.Value
end

(where inventory is a folder of values, although you could achieve a similar result using module scripts)

You can store functions or just data in module scripts. For example:

local module = 
{["Prices"] = {
				["Fruit"] = {	
								["Apple"] = 10,
								["Orange"] = 20,
								["Coconut"] = 30,
								["Mango"] = 40
								},
				["Packs"] = {
								["Starter Bag"] = 0,
								["School Bag"] = 10,
								["Duffle Bag"] = 20,
								["Adventure Bag"] = 30,
								["Basket Bag"] = 40
								}}

return module
1 Like

And how do you get the functions / data?

You should read the module script API reference.

You can get the module in any script depending on it’s location:

local modulescript = require(ServerStorage.modulescript)

print(modulescript.Prices.Fruit.Orange)
-- prints the value of Orange


2 Likes

So if modulescripts are meant to return data, what use does that have for this?

i just realised it would return the values, but can it return a property? or just the value of said property?

If you didn’t want to update values directly from the datastore when the player is added, other scripts could access the module script and change values accordingly.

You can change properties based on the information stored in the tables of the datastore or module script. Like for example:

local PriceLabel = PlayerGUI:WaitForChild("PriceLabel")
local modulescript = require(ServerStorage.modulescript)

PriceLabel.Text = tostring(modulescript.Prices.Fruit.Orange)
1 Like

Ok i think i get it now, i can make a dictionary in the datastore and then use it as an argument in a moduleScript to set the card’s stats accordingly and then return a reference to the now complete card.

Yes, kind of. You’ve got the idea anyway.