Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

DataStore making all values into one?

Asked by 4 years ago
Edited 4 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

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

01game.ReplicatedStorage.SaveBuild.OnServerEvent:Connect(function(player)
02    local partTable = {}
03 
04    for i, inst in pairs(workspace:GetChildren()) do
05        if inst.Name == "BuiltBlock" then
06            local position = {
07                X = inst.Position.X,
08                Y = inst.Position.Y,
09                Z = inst.Position.Z
10 
11            }
12            local material = inst.Material
13            local color = inst.BrickColor
14            local size = inst.Size
15            partTable[i] = {Position = position, Material = tostring(material), Color = tostring(color), Size = tostring(size)}
View all 28 lines...

Load Script:

01game.ReplicatedStorage.LoadBuild.OnServerEvent:Connect(function(player)
02    local s,e = pcall(function()
03        local build = BuildsDataStore:GetAsync(player.UserId)
04        for i, tab in pairs(build) do
05            local block = Instance.new("Part",workspace)
06            block.Name = "BuiltBlock"
07            block.Position.X = tonumber(tab.Position.X)
08            block.Position.Y = tonumber(tab.Position.Y)
09            block.Position.Z = tonumber(tab.Position.Z)
10            block.Size = tonumber(tab.Size)
11            block.Material = tab.Material
12            block.BrickColor = tab.Color
13        end
14    end)
15    if s then
16        print("Successfully loaded build!")
17    elseif not s and e then
18        print("Not successful: "..e)
19    end
20end)

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

1 answer

Log in to vote
0
Answered by 4 years ago

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.

0
I just wanna add a comment here. The size property was optional since I made all the blocks the same size: 4,4,4. And also, thank you for this response! It helped me A BUNCH! DragonSpawner12 61 — 4y
Ad

Answer this question