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

DataStore not getting or saving array correctly?

Asked by 4 years ago

I’m trying to make a sort of RTS DataStore where Units are stored. Upon Getting the datastore value, it always returns an empty array. I added a couple of print statements and proved that the array returned from the datastore is empty. I would love some help or insight into this Code:

local DataStore = game:GetService("DataStoreService")
local UnitStore3 = DataStore:GetDataStore("UnitDataStore")
game.Players.PlayerAdded:Connect(function(plr)
    repeat wait() until plr.Character
    local OwnedUnits = Instance.new("Folder",workspace)
    OwnedUnits.Name = plr.Name.."_Units"
    local Units
    local success, eror = pcall(function()
        local UUnits =  UnitStore3:GetAsync("OwnedUnits_"..plr.Name)
        Units = game:GetService("HttpService"):JSONDecode(UUnits)
        print(#Units)
    end)
    if success then
        if #Units  ~= 0 then
            for k,v in pairs(Units) do
                local NewUnit = game.ReplicatedStorage.Units:FindFirstChild(v.Name)
                NewUnit = NewUnit:Clone()
                NewUnit.Parent = OwnedUnits

                NewUnit:MoveTo((plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,2).Position))
            end
        else
            local NewUnit = game.ReplicatedStorage.Units.Swordsman:Clone()
            NewUnit.Parent = OwnedUnits
            NewUnit:MoveTo((plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,2).Position))
            local SaveUnits = {}
            for k,v in pairs(OwnedUnits:GetChildren()) do
                table.insert(SaveUnits,v)
                print(v.Name)
            end

            local Encoded = game:GetService("HttpService"):JSONEncode(SaveUnits)
            UnitStore3:SetAsync("OwnedUnits_"..plr.Name, Encoded)

        end
    elseif eror then
        local SaveUnits = {}
            for k,v in pairs(OwnedUnits:GetChildren()) do
            table.insert(SaveUnits,v.Name)
            print(v.Name)
            end
        local NewUnit = game.ReplicatedStorage.Units.Swordsman:Clone()
        NewUnit.Parent = OwnedUnits
        NewUnit:MoveTo((plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,2).Position))
        UnitStore3:SetAsync("OwnedUnits_"..plr.Name,game:GetService("HttpService"):JSONEncode(SaveUnits:GetChildren()))
    end


end)

game.Players.PlayerRemoving:Connect(function(plr)
    local OwnedUnits = workspace:FindFirstChild(plr.Name.."_Units"):GetChildren()
    local SaveUnits = {}
    for k,v in pairs(OwnedUnits:GetChildren()) do
        table.insert(SaveUnits,v)
    end
    UnitStore3:SetAsync("OwnedUnits_"..plr.Name,game:GetService("HttpService"):JSONEncode(SaveUnits:GetChildren()))
end)

0
I would highly recommend using UpdateAsync over SetAsync when saving players data, your problem could be a result of that. Have you tried testing it in game? And have you tried adding a BindToClose() to save player's data? Nickuhhhhhhhhhhhhhhh 834 — 4y

Answer this question