Problem with datastore table saving

I’m trying to save datastore tables; which are important for my development project since I also need to save data of the player / edit it too.
The work I am trying to do is a Vehicle Ownership datastore.
Everything works fine no errors; it’s a problem with datastore and it just only saves one value when I buy a car; and it thinks that the other car is same as the value but instead the name of the car and the strings and information are different.

Server Script:

local remote = game.ReplicatedStorage.BuyCar
local remote2 = game.ReplicatedStorage.CheckIfOwned
local module = require(game.ServerScriptService.CarData)
local prompt = game.ReplicatedStorage.Prompt
local datastore = game:GetService("DataStoreService")
local data = datastore:GetDataStore("BoughtCars")
local data2 = nil
local data3 = nil

remote.OnServerInvoke = function(plr, stringcode)
    local d,p = pcall(function()
        data3 = data:GetAsync(plr.UserId)
    end)
    if data3 ~= nil then 
        if not table.find(data3.OwnedCars, stringcode) then
            if stringcode and plr then 
                for i,v in module.Enums.Normal do 
                    if v[tostring(stringcode)] then 
                        if v[tostring(stringcode)].Price <= plr.leaderstats.Money.Value then 
                            plr.leaderstats.Money.Value = plr.leaderstats.Money.Value - tonumber(v[tostring(stringcode)].Price)
                            local s,r = pcall(function()
                                data2 = data:SetAsync(plr.UserId, {OwnedCars = {stringcode}})

                            end)
                        end
                    end
                end

            else 
                assert(stringcode, stringcode .. "not found. (table identifier)")
            end
        elseif table.find(data3.OwnedCars, stringcode) then
            print("Already bought.")
        end
    else 
        if stringcode and plr then 
            for i,v in module.Enums.Normal do 
                if v[tostring(stringcode)] then 
                    if v[tostring(stringcode)].Price <= plr.leaderstats.Money.Value then 
                        plr.leaderstats.Money.Value = plr.leaderstats.Money.Value - tonumber(v[tostring(stringcode)].Price)
                        local s,r = pcall(function()
                            data2 = data:SetAsync(plr.UserId, {OwnedCars = {stringcode}})

                        end)
                    else 
                        game.ReplicatedStorage.R2:FireClient(plr)
                    end
                end
            end
        end
    end




    return data2



end

remote2.OnServerInvoke = function(plr)
    local d,p = pcall(function()
        data3 = data:GetAsync(plr.UserId)
    end)
    if data3 ~= nil then 
    for i,v in data3.OwnedCars do
       print(v)
    end
    end
    return data3
end

ModuleScript Data:

local cardata = {}

cardata.Enums = {
    Normal = {
        Nissan = {
            ["Y60"] = {
                Price = 27500;
                EOName = "Y60"

            };
            ["NissanPatrolSSWhiteOFFRoad"] = {
                Price = 25000;
                EOName = "NissanPatrolSSWhiteOFFRoad"
            };
            

        };
        Lexus = {

        };
        Honda = {

        };
        BMW = { 

        };
        Mitsubishi = {

        };
        Volvo = { 

        };
        Motorbikes = {

        };
        Buggy = {

        };
        Toyota = {
            ["LandCruiser"] = {
                Price = 30000;
                EOName = "LandCruiser"
            };
            ["LC100"] = {
                Price = 28000;
                EOName = "LC100"
            };
            ["LCGXR"] = {
                Price = 28000;
                EOName = "LCGXR"
            };
        };
        Suzuki = {

        }
    };
}



return cardata
local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnCarEvent = ReplicatedStorage:WaitForChild("SpawnCar")
local TS = game:GetService("TweenService")
local SVE = ReplicatedStorage.CheckIfOwned
local DeleteCarEvent = ReplicatedStorage:WaitForChild("DeleteCar")
local prompt = game.ReplicatedStorage.Prompt
local BuyCar = ReplicatedStorage.BuyCar
local carName = script.Parent.Parent.Name
local SpawnCarFrame = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent
local owned = SVE:InvokeServer("NissanPatrolSSWhiteOFFRoad")


script.Parent.MouseButton1Click:Connect(function()
    if owned ~= nil then 
        if owned.OwnedCars then 
            SpawnCarFrame.Visible = false
            task.wait()
            SpawnCarEvent:FireServer("Admin Car Ace Country")
        elseif tostring(owned.OwnedCars) ~= "NissanPatrolSSWhiteOFFRoad" then 
            local rep = BuyCar:InvokeServer("NissanPatrolSSWhiteOFFRoad")
            if rep then 
                script.Parent.Text = "SPAWN"
                SpawnCarEvent:FireServer("Admin Car Ace Country")
            else 
                TS:Create(script.Parent, TweenInfo.new(0.8, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {TextColor3 = Color3.fromRGB(255,0,0)}):Play()
                prompt:FireServer(script.Parent.Text)
                task.wait(4)
                TS:Create(script.Parent, TweenInfo.new(0.8, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {TextColor3 = Color3.fromRGB(15, 59, 0)}):Play()
            end
        else 
            local rep = BuyCar:InvokeServer("NissanPatrolSSWhiteOFFRoad")
            if rep then 
                script.Parent.Text = "SPAWN"
                SpawnCarEvent:FireServer("Admin Car Ace Country")
            else 
                TS:Create(script.Parent, TweenInfo.new(0.8, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {TextColor3 = Color3.fromRGB(255,0,0)}):Play()
                prompt:FireServer(script.Parent.Text)
                task.wait(4)
                TS:Create(script.Parent, TweenInfo.new(0.8, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {TextColor3 = Color3.fromRGB(15, 59, 0)}):Play()
            end
        end
    else 
        local rep = BuyCar:InvokeServer("NissanPatrolSSWhiteOFFRoad")
        if rep then 
            script.Parent.Text = "SPAWN"
            SpawnCarEvent:FireServer("Admin Car Ace Country")
        else 
            TS:Create(script.Parent, TweenInfo.new(0.8, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {TextColor3 = Color3.fromRGB(255,0,0)}):Play()
            prompt:FireServer(script.Parent.Text)
            task.wait(4)
            TS:Create(script.Parent, TweenInfo.new(0.8, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {TextColor3 = Color3.fromRGB(15, 59, 0)}):Play()
        end
    end
end)


Output: 15:22:47.381 ▼ NissanPatrolSSWhiteOFFRoad (x5) - Server - BuyCar:68
15:22:47.381 NissanPatrolSSWhiteOFFRoad
15:22:47.381 NissanPatrolSSWhiteOFFRoad
15:22:47.415 NissanPatrolSSWhiteOFFRoad
15:22:47.551 NissanPatrolSSWhiteOFFRoad

Also; the Admin Ace Car County thing is a free model. It’s just for test purposes and I don’t use it in realtime development.

So if you are willing to help me, I’ll be glad!

oh my thats a lot of if statements, maybe you should consider doing “if not variable then return end” statements

anyway i’ve also recently come upon this issue trying to save player’s builds in my game and the solution i found is using: HTTPService:JSONEncode() which i used like this

playerData:SetAsync(player.UserId…“/SaveData”, {Wood = wood, Stone = stone, Builds = HTTP:JSONEncode(builds)})

for context “builds” is table that contains a dictionary of a building’s name, level, and position for each building

edit: oh yeah and if you want to decode the table its HTTPService:JSONDecode()

The output said nothing. Just nil, upon using GetAsync on my UserID

local E = {}
 table.insert(E, stringcode)
 data2 = data:SetAsync(plr.UserId, {OwnedCars = {HTTPS:JSONEncode(E)}})

(stringcode) is not a table. It’s a string that I am trying to send to server.

hmm try removing the brackets on the encode function like this
{OwnedCars = HTTPS:JSONEncode(E)})

Thanks dude, it finally worked; with using table.insert() on the client!

1 Like

glad to help :wink:

minimum characters moment

1 Like

The stuff works; but my question is how to stop it overriding when I buy a car to not loose my data? It keeps overriding when I buy a new car.

From this point when I RemoveAsync() my data and joined first buying two cars at twice it saves like this
{
[1] = “NissanPatrolSSWhiteOFFRoad”,
[2] = “Y60”
}

However if I buy one car it will stay like this
{
[1] = “NissanPatrolSSWhiteOFFRoad”,
};
Overriding my stuff

And the next time I buy the car when rejoining the game it overrides to this:
{
[1] = “Y60”
}

1 Like

Solved! Used UpdateAsync. Thanks bro, I appreciate it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.