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

table.insert resets some values to 0?

Asked by 2 years ago

For some reason when I try to insert data into a table using table.insert, the x,y, and z positions get reset to 0. I have checked the values for x, y, and z outside of the table.insert, and they seem to work, but in the table, the values return as 0.

the defective part:

for i, obj in ipairs(plot.itemHolder:GetChildren()) do
            table.insert(data, {
                ["name"] = obj.Name,
                ["transform"] = {
                    ["x"] = obj.PrimaryPart.CFrame.X;
                    ["y"] = obj.PrimaryPart.CFrame.Y;
                    ["z"] = obj.PrimaryPart.CFrame.Z;
                    ["r"] = obj.PrimaryPart.Orientation.Y
                }
            })

        end

the whole code:

local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")

local plotManager = require(script.Parent.ServerModules.PlotManager)
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Bleep")

local tries = 3
local dataloaded = false

local function set(plr)
    if dataloaded then
        local plot = plotManager.returnPlot(workspace.plots, plr)

        local key = plr.UserId
        local count = 0

        local data = {}

        for i, obj in ipairs(plot.itemHolder:GetChildren()) do
            table.insert(data, {
                ["name"] = obj.Name,
                ["transform"] = {
                    ["x"] = obj.PrimaryPart.CFrame.X;
                    ["y"] = obj.PrimaryPart.CFrame.Y;
                    ["z"] = obj.PrimaryPart.CFrame.Z;
                    ["r"] = obj.PrimaryPart.Orientation.Y
                }
            })

        end

        print(data)

        local success, err

        repeat
            success, err = pcall(function()
                dataStore:SetAsync(key, data)
            end)

            count = count + 1
        until count >= tries or success

        if not success then
            warn("Data could not be set." .. tostring(err))

            return
        end
    else
        warn("Data has not been loaded. Do not attempt to set data when it has not been loaded.")

        return
    end
end

local function get(plr)

    local key = plr.UserId
    local count = 0

    local data

    local success, err

    repeat
        success, err = pcall(function()
            data = dataStore:GetAsync(key)
        end)

        count = count + 1
    until count >= tries or success

    if not success then
        warn("Failed to read data." .. tostring(err))

        return
    end

    if data then
        local plot = plotManager.returnPlot(workspace.plots, plr)
        for i, saved in ipairs(data) do
            local loadedModel = replicatedStorage.models:FindFirstChild(saved.name):Clone()

            if loadedModel then
                loadedModel:SetPrimaryPartCFrame(CFrame.new(saved.transform.X, saved.transform.Y, saved.transform.Z)*CFrame.Angles(0, math.rad(saved.transform.r), 0))

                loadedModel.Parent = plot.itemHolder
            else
                return
            end
        end
        dataloaded = true

        return data
    else
        dataloaded = true

        return {}
    end
end

local function unloadTycoonData(plr)
    local plot = plotManager.returnPlot(workspace.plots, plr)

    set(plr)

    for _, obj in ipairs(plot.itemHolder:GetChildren()) do
        obj:Destroy()
    end

    plot.owner.Value = ""
end

players.PlayerAdded:Connect(get)
players.PlayerRemoving:Connect(unloadTycoonData)

game:BindToClose(function()
    for i, plr in ipairs(players:GetPlayers()) do
        set(plr)
    end
end)

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

I believe I fixed the issue. The issue had nothing to do with the table, and it was with the data store.

Ad

Answer this question