So i made a article on this yesterday about saving the build. It worked (thanks guys!). But, there is a big flaw when the DataStore saves the data. It makes all the values I want to save in to one? So that means I can't load the build without it having a error because of the Position value I saved since it add the values up. How can I make the dictionary of values not turn in to one value? Here are the links to the scripts I have:
Save Script: https://pastebin.com/Y2W3kckk
game.ReplicatedStorage.SaveBuild.OnServerEvent:Connect(function(player) local partTable = {} for i, inst in pairs(workspace:GetChildren()) do if inst.Name == "BuiltBlock" then local position = { X = inst.Position.X, Y = inst.Position.Y, Z = inst.Position.Z } local material = inst.Material local color = inst.BrickColor local size = inst.Size partTable[i] = {Position = position, Material = tostring(material), Color = tostring(color), Size = tostring(size)} end end local success, err = pcall(function() BuildsDataStore:SetAsync(player.UserId, partTable) end) if success then print("Saved player build.") elseif not success and err then print("Not successful: "..err) end end)
Load Script:
game.ReplicatedStorage.LoadBuild.OnServerEvent:Connect(function(player) local s,e = pcall(function() local build = BuildsDataStore:GetAsync(player.UserId) for i, tab in pairs(build) do local block = Instance.new("Part",workspace) block.Name = "BuiltBlock" block.Position.X = tonumber(tab.Position.X) block.Position.Y = tonumber(tab.Position.Y) block.Position.Z = tonumber(tab.Position.Z) block.Size = tonumber(tab.Size) block.Material = tab.Material block.BrickColor = tab.Color end end) if s then print("Successfully loaded build!") elseif not s and e then print("Not successful: "..e) end end)
Error when loaded: Not successful: X cannot be assigned to
Data that was saved in the block tables: https://i.imgur.com/WdY9wr1.png
Please answer my question!! -- DragonSpawner12 :D
Vector3.X/Y/Z are read-only properties, you may not mutate vectors, only create new ones. Instead a new vector should be made with the X/Y/Z coordinates being assigned using the appropriate constructor Vector3.new(X ,Y, Z) (in your case block.Position
is the vector, you'd do block.Position = Vector3.new(X ,Y, Z)
)
Also, you don't need to (re)convert the coordinates to numbers at all! Types do not change while saving/retrieving, tab.Position.X/Y/Z
will be a number since it's saved as one.
Another thing I noticed is that you're wrongly assuming BasePart.Size to be a single number, while it is in fact a Vector3 type similar to BasePart.Position, you'll have to implement the same method you did with position.