Lately, I have been working on a game. It has to save an objects position like one in miners haven... I am making Instances and turning them into Vector 3 Values to save them and so far it is giving me an error saying "Cannot Store Array In DataStore" Can someone help me?
script error..
01 | local DataStore = game:GetService( "DataStoreService" ):GetDataStore( "HelpMePlease" ) |
02 |
03 | game.Players.PlayerRemoving:connect( function (player) |
04 | local key = "player-" ..player.userId |
05 | for _, v in pairs (player.HiddenStats.ItemPos:GetChildren()) do |
06 | print ( "Saving" ) |
07 | valuesToSave = { player.HiddenStats [ "Item Data Store Name" ] .Value, v.Value } -- I cannot make this local, It has to be global |
08 | print (valuesToSave) |
09 | end |
10 | DataStore:SetAsync(key, valuesToSave) |
11 | end |
12 | end ) |
the data store error is the DataStore:SetAsync(key, valuesToSave) although it should save.. API is on.
Thanks for any help
So in order to save a Vector3 value you'll have to save it as a table.
01 | local function serializeVector 3 (vector 3 ) |
02 | return { vector 3. x,vector 3. y,vector 3. z } |
03 | end |
04 | local function deserealizeVector 3 (serealizedvector 3 ) |
05 | return Vector 3. new( unpack (serealizedvector 3 ) |
06 | end |
07 | --example |
08 | local vector 3 = Vector 3. new( 1 , 2 , 3 ) |
09 |
10 | local vector 3 holder = serialize(vector 3 ) |
11 |
12 | local unpackvector 3 = deserealize(vector 3 holder) |
13 | print (unpackvector 3 ) -- Vector3.new(1,2,3) |
14 | print (vector 3 holder) -- table: 2F9381DC |
15 |
16 | --saving |
17 | for i,v in next ,DataStore do |
18 | DataStore [ "Vector3Value" ] = unpackvector 3 |
19 | end |
You can do this with any Vector3/Color3/ value. --Credits go to. https://scriptinghelpers.org/questions/51612/how-to-save-a-color3-value
Answer by Radstar1 works, but you can also use the tostring() function and some simple interpolation to translate the text into a vector3 value.