Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What would be the best way to save data like this?

Hey!

The situation I'm going to describe is not the complete idea of what I am planning on doing, but if I could get an answer for this situation then I should be able to figure out how to do it myself :)

The image here is 12,500 blocks, with randomly generated colours. Hypothetically, they also have many other variables associated to the brick, in this example we can use a random number for each brick needs to be saved, along with the age of the brick and, of course, it's colour.

If I wanted to save this data in a data store, so I could leave the game and join back then recreate the same image with an automated function.. How would I go about doing this?

I was thinking of hash tables, but I'm not sure if that is the most efficient or effective method. Please could you suggest the best methods for this that you know (And preferably any information on the methods)?

Thanks so much!!

1 answer

✓ Accepted answer

#What could we do Save the data as a Table, but not a regular table, a Dictionary. Tables can also save tables inside of them.


#Tables You probably already know how to make Tables and Dictionaries

local table = {"I am a table", false, "you are a string", 231}

local dictionary = {["Tintinnabulation"] = "The ringing sounds in your ears",
["Abecedarian"] = "Beginner",
["LordDragonZord"] = "English grades are bad but would rather learn English than math."}

Here's the difference between tables and dictionaries. Tables have iterators already set for them which is 1-#table (1 through the number of values in the table). The iterators for dictionaries are set by you. Meaning you can have custom iterators. Another thing is if you want to add values to them you do them in different ways:

table.insert(Table, "Foo")

dictionary["And I would walk 500 miles"] = "and I would walk 500 more"
--Note tables' values can also be added the same method
table[1] = "and I would walk 1000 miles, to see your face just passing by"


#Final Product This is what your script should look like, this is like the blue prints. Base your script off this one.

local ds = game:GetService("DataStoreService"):GetDataStore("Drawing")
--[[plyrdata = {["Part1"] = {["BrickColor"] = BrickColor.new(1),
["Size"] = Vector3.new(),
["CFrame"] = CFrame.new()}}
Save player data like so ^^]]
local datatobesaved = {"BrickColor", "Size", "CFrame"} --List of values that you want to be saved

function setplyrdata(objs, id) --Fills up the table with information
	local table = {}
		for _,p in pairs(objs:GetChildren()) do
			table[p.Name] = {}
			for _,prop in pairs(datatobesaved) do
				table[p.Name][prop] = p[prop]
			end
		end
	end
	return table
end

game:GetService("Players").PlayerAdded:connect(function(plyr)
	local plyrdata = ds:GetAsync(plyr.userId)
	local palate = workspace.PlayersBlocks --Change that
	if plyrdata then
		for i,v in pairs(plyrdata) do
			for _,p in pairs(palate:GetChildren()) do
				if p.Name == i then
					for prop,val in pairs(v) do
						p[prop] = val --Changing the part's property listed earlier to it's value.
					end
				end
			end
		end
	else
		ds:SetAsync(plyr.userId, setplyrdata(palate, plyr.userId)) --Save the table
	end
end)


Hope it helps!(Edit the final product to match your script)

1 comment

0

Extremely useful answer, thanks! I will attempt to adapt this for what I need, as I didn't specify exactly what I needed. Is it OK if I can message you in regard to this post later on, with more detail on certain situations? :) seriously useful answer thought, thanks

This discussion is preserved as it was. New answers, comments, and votes are closed.