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

DataStore no errors, but also not saving, loading?

Asked by
dirk2999 103
5 years ago

The points are saving. The carts are not. So basically when the player joins, I take all the carts that are in a folder, and make BoolValues for each of them into the player. When the player leaves, it checks which BoolValues are true (if their true the player purchased the cart) and inserts them into a table to be saved. When the player rejoins, if their's any carts in the table, once the BoolValues are created, it sets the BoolValues with the same name to true in the player. I don't see any errors in the output, but the carts do not save. This is my first attempt with using DataStore, so I apologize if I made a rookie mistake.

local DataStore = game:GetService("DataStoreService")
local dataPoints = DataStore:GetDataStore("testPoints")
local cartSaves = DataStore:GetDataStore("testCartSaves")

game.Players.PlayerAdded:connect(function(plr)
    local cartsTable = {}
    local points = Instance.new("IntValue",plr)
    points.Name = "Points"
    points.Value = dataPoints:GetAsync(plr.userId) or 0
    local cartsFolder = Instance.new("Folder",plr)
    cartsFolder.Name = "Carts"
    local getCarts = cartSaves:GetAsync(plr.userId)
    if getCarts then
        table.insert(cartsTable,getCarts)
    end
    for _,Carts in pairs(game.ReplicatedStorage.carts:GetChildren()) do
        local newBool = Instance.new("BoolValue",cartsFolder)
        newBool.Name = Carts.Name
    end
    if #cartsTable > 0 then
        for i = 1,#cartsTable do
            if plr.Carts:FindFirstChild(cartsTable[i]) then
                plr.Carts:FindFirstChild(cartsTable[i]).Value = true
            end
        end
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    dataPoints:SetAsync(plr.userId,plr.Points.Value)
    local cartsSaveTable = {}
    for _,Cart in pairs(plr.Carts:GetChildren()) do
        if Cart.Value == true then
            table.insert(cartsSaveTable,Cart.Name)
        end
    end
    cartSaves:SetAsync(plr.userId,cartsSaveTable)
end)
0
connect is deprecated, use Connect, userId is deprecated, use UserId, the parent parameter to Instance.new is deprecated User#19524 175 — 5y
0
Instance.new(classname,parent) isn't deprecated, it's just more taxing on the system because it's trying to change the values of an already existing object. Phantom1996 45 — 5y
0
It is deprecated. User#19524 175 — 5y

Answer this question