Okay, I know someone already made a tutorial about this, but it wasn’t really clear and this tutorial shows the easiest way of saving values like,
- Vector3.new
- Cframe
- Color3
and etc.
Now you might easily just SetAsync right? Boom. An error popped up.
Why?
Because datastore can only save UTF-8 characters, which means it only stores strings, numbers and text, which means vector3,cframe is not a UTF-8 character
If you follow the 2 steps, you will be able to save positions, colors, etc.
Step 1 - Converting the unsupported value to a table
Datastores can also save tables, which is basically a lifesaver for me, so I want to save a color3 to the datastore, as said. It will print an error
(That’s only 1 line, pretty easy right?)
game:GetService("DataStoreService"):GetDataStore("Colors"):SetAsync("plr",{R = block.Color3.R, G = block.Color3.G, B = block.Color3.B})
The more arguments, the more arguments you need to add in the table.
basically depending on arguments.
yay no more errors
Step 2 - Reconverting table to the unsupported value
Okay, since you saved it to the datastore successfully, you’re gonna hype to :GetAsync() the color key
But wait… it’s always returns the black color, I wanted white!
That’s because that now you saved it as table, you need to make it color3 again.
Now I want the brick to be white
local retrieveddata = game:GetService("DataStoreService"):GetDataStore("Colors"):GetAsync("plr")
-- i added local because ill not retrieve data 3 times
game.Workspace.brick.Color3 = Color3.new(retrieveddata.R,retrieveddata.G,retrieveddata.B)
Yay, now it’s white!
Now you’re able to load and save vector3, cframes into datastore, ofcourse there are other ways to do it but I think this is the easiest
Bonus : Saving enums + loading them
Okay, you can now save parts to datastore, but what if you wanna save the material too? You can’t do that with tables. And that’s why I made the bonus
So?
Enums can’t also be saved in datastore as it’s not an UTF-8 character too
But you can’t convert it to table either, so we’re gonna convert it to string instead
This function returns the enum as a string.
It’s also 1 line
So , saving the enum.
game:GetService("DataStoreService"):GetDataStore("material"):SetAsync("ilikeplastic",part.Material.Name)
Now for the loading
part.Material = Enum.Material[game:GetService("DataStoreService"):GetDataStore("material"):GetAsync("ilikeplastic")]
cool, now you can save all kinds of unsupported values completely
Saving the value and loading them without using dictionaries
This section is suggested by @jelliedbanana1
This is for people who worries about the character limit. If you’re not worrying ab it or is just saving with part limit etc this shouldn’t matter unless it’s huge. The datastore character limit is like 2 MB which is a good thing, it’s huge, however what if you’re making a saving huge buildings system, like what if the map is too huge for it to be saved with dictionaries?
Ok now this method does not do a big change comparing to the dictionaries but hey, it does some free space.
So let’s make it into a table.
game:GetService("DataStoreService"):GetDataStore("Colors"):SetAsync("plr",{block.Color3.R,block.Color3.G,block.Color3.B})
You might see no difference right? We removed the spaces and the R =, G = , B = .
Now for the loading, we have two ways.
- Table.unpack
local retrieveddata = game:GetService("DataStoreService"):GetDataStore("Colors"):GetAsync("plr")
game.Workspace.brick.Color3 = Color3.new(table.unpack(retrieveddata))
- I have no idea what to name this
local retrieveddata = game:GetService("DataStoreService"):GetDataStore("Colors"):GetAsync("plr")
game.Workspace.brick.Color3 = Color3.new(retrieved data[1],retrieveddata[2],retrieveddata[3])
Although I wouldn’t recommend the 2nd way for saving maps, I recommend table.unpack instead.
Sorry for not being indepth as it already is obvious on what it does, I am just lazy
Yeah, that was quick and an easy tutorial, you can send feedback s