DataStore and DataStore2

i wanted to use saving in my game in order to save customized animated characters. i had basically used a little template that went like this:

charabase = {
    idle1 = "",
    idle2 = "",
    walk = {
        walk1 = "",
        walk2 = "",
        walk3 = "",
        walk4 = ""}
    jump = "",
    fall = "",
    fly = {
        fly1 = "",
        fly2 = "",
        fly3 = "",
        flyidle = ""}
    offset = 0,
    scale = Vector2.new(0, 0),
    idlespeed = 0,
    walkspeed = 0,
   name = ""}

and had it repeat in another array. i wrote a function to basically duplicate these values into the newer tables, so i wouldn’t have to copy+paste and waste up lines. it was like this:

local data = {
	SavedCharas = {
		["chara1"] = {},
		["chara2"] = {},
		["chara3"] = {},
		["chara4"] = {},
		["chara5"] = {},
		["chara6"] = {},
		["chara7"] = {},
		["chara8"] = {},
		["chara9"] = {},
		["chara10"] = {}
	},
	Stats = {
		["Time"] = 0,
		["Money"] = 0
	}
}
for i, v in pairs(data.SavedCharas) do --BECAUSE I DON'T WANT TO WASTE LINES SETTING UP SO MUCH!!!!
	for j, n in pairs(charabase) do
		v[j] = n
	end
end

i was using datastore2 for the above, since it seemed to use backups, something i liked. however, as i started experimenting, i started to get errors and eventually lost myself in this sea of code. i had no idea how to work datastore or datastore 2, which, i suppose is my mistake. i used wikis and youtube tutorials, but i still don’t really understand it.
basically, i want to save tables in tables. unecessary, maybe? perhaps, but i don’t really know other ways to do this.
i’m not asking for somebody to do this for me, rather, how do i even begin?

You need to use a copy table function. Right now, the way you’re setting values in your saved characters tables just puts pointers towards the same values in the charabase table, so nothing actually gets copied over.

Here is a resource on copy table functions:
http://lua-users.org/wiki/CopyTable

Beyond that, there’s a lot of tutorials you can find on YouTube, the DevForum, Toolbox and the Developer Hub. If you couldn’t find anything or you don’t understand, you haven’t consulted enough resources or may need to change your approach.

3 Likes

I find it hard to understand the post but going off what colbert’s understanding of it is, if you want to have a template table with predetermined values to copy around easily you can just throw it into a function.

local function a_template()
    return {
        SavedCharas = {
            ["chara1"] = {},
            ["chara2"] = {},
             -- etc
        },
        Stats = {
            -- more stuf...
        }
    }
end

What happens here is each time your function is run, it creates a new table in that same format but separate of the others.

ah!!! thank you. i really appreciate this
dumb question. since i noticed that when i print a table, it’ll print its hex value or whatever it’s called. could i potentially set a stringvalue to that, and have another script read off of it?

No, the hex value just represents its place in memory, you have to pass around the actual table still.

1 Like

gotcha. that should be easy for me to do
thank you!