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

DataStore object saving doesn't save?

Asked by 4 years ago
Edited 4 years ago

Ok, so...Hi. I have some problems with my Data store...For the 10th time I ever try to save something...

Here's a more detailed explanation of the issue:

Error I get: ServerScriptService.DataStoreScript:18: attempt to index nil with 'GetChildren'

About the error: I have a folder that should be looped thru when the player leaves the game. That folder(as I test) has objects inside of it. They ARE in the server and they DO exist, but for some reason the script doesn't see the folder.

Where have I tested the script? In studio and in game. I don't know about you, but for me the data stores save in studio as long as I have the *API *and *HTTP * on.

What am I trying to even save?

The object's Name, CFrame(made into X, Y, Z and Orientation values)

Got any other questions?

Ask ahead. I will try and answer them as good and detailed as possible. Tho I hope you don't have because it's kinda late for me now xd (12am) and might not even see them, so if I don't see them you'd loose interest in helping me and forget about this.

Script:

local Players = game.Players
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlotSave")
local Plots = game.Workspace.Plots:GetChildren()


game.Players.PlayerRemoving:Connect(function(player)
    local Key = player.UserId
    local SaveTable = {}
    local ObjectsFolder

    for i,v in pairs(Plots) do
        if v.Owner.Value == player then
            ObjectsFolder = v.PlacedObjects
            break
        end
    end
    for i,v in pairs(ObjectsFolder:GetChildren()) do
        if v then
            table.insert(SaveTable,
                {
                    ["ObjectName"] = v.Name,
                    ["ObjectCFrame"] = {
                        ["X"] = v.PrimaryPart.CFrame.X, 
                        ["Y"] = v.PrimaryPart.CFrame.Y, 
                        ["Z"] = v.PrimaryPart.CFrame.Z, 
                        ["Rotation"] = v.PrimaryPart.Orientation.Y}
                }
            )
        end
    end

    local Success, ErrorMessage = pcall(function()
        DataStore:SetAsync(Key, SaveTable)
    end)
    if Success then
        print("Successfully saved Data")
        for i,v in pairs(SaveTable) do
            print(v)
        end
    else
        warn("Failed to save Data: ".. tostring(ErrorMessage))
        return;
    end
end)

game.Players.PlayerAdded:Connect(function(player)
    local Key = player.UserId
    local SavedPlot
    local ObjectsFolder

    for i,v in pairs(Plots) do
        if v.Owner.Value == player then
            ObjectsFolder = v.PlacedObjects
            break
        end
    end
    local Success, ErrorMessage = pcall(function()
        SavedPlot = DataStore:GetAsync(Key)
    end)

    if Success then
        print("Data Loading")
    else
        warn("Data Failed to be read: ".. tostring(ErrorMessage))
    end

    if SavedPlot then
        wait(1)
        print("There is a Saved Plot")
        for i,v in pairs(SavedPlot) do
            if v then
                print(SavedPlot[i])
                local SavedModel = game.ReplicatedStorage.Objects:FindFirstChild(v.Name):Clone()
                if SavedModel then
                    SavedModel:SetPrimaryPartCFrame(CFrame.new(v.ObjectCFrame.X, v.ObjectCFrame.Y, v.ObjectCFrame.Z) * CFrame.Angles(0, math.rad(v.ObjectCFrame.Rotation), 0))
                    SavedModel.Parent = nil
                end
            else
                print("No Objects")
            end
        end
    else
        print("No Saved Plot")
    end
end)
0
objectsfolder is nil srimmbow 241 — 4y
0
Line 14 should make it not nil bostaffmanbulgaria1 89 — 4y

1 answer

Log in to vote
1
Answered by
Elyzzia 1294 Moderation Voter
4 years ago

when you do local Plots = workspace.Plots:GetChildren() at the start of the script, it only gets the children at that specific time. it doesn't automatically update tables when you add or remove children

of course, when a server first opens and this script runs, nothing is going to be in the Plots folder

you have to get the children of Plots every time you want to save a player's data

for i,v in pairs(workspace.Plots:GetChildren()) do
    if v.Owner.Value == player then
        ObjectsFolder = v.PlacedObjects
        break
    end
end
0
Imma try it out when i got some time, thanks for the help bostaffmanbulgaria1 89 — 4y
Ad

Answer this question