Need Help With My DataStore

Hi, there my datastore script gives no errors and I’m trying to save cars and cash at the moment and I just can’t figure it out so I came to Devforums.

I have been scripting for a Few weeks and I’m trying to learn Datastores to get it under my wing since i feel its one of the most important parts of a game, and the better i understand it from early stages the better i will get down the line

EDIT: if possible explain to me what I did wrong and how to avoid doing it in the future

Script:

  local DatastoreService = game:GetService("DataStoreService")
local DataStore = DatastoreService:GetDataStore("PlayerData")

local function Load(player)
local key = "gamer"..player.UserId
local carsSaved = DataStore:GetAsync(key,"-ownedCars") or {}
local cashSaved = DataStore:GetAsync(key.."-Playercash")

local stats = Instance.new("Folder")
stats.Name = "PlayerData"
stats.Parent = player


local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = stats

local cars = Instance.new("Folder")
cars.Name = "OwnedCars"
cars.Parent = player

if cashSaved then
	cash.Value = cash.Value
else
	cash.Value = 3000
end

for _, carSaved in pairs(carsSaved) do
	if cars:FindFirstChild(carSaved) then
		cars[carSaved]:Clone().Parent = player.OwnedCars
	end
end
end

local function Save(player)
local key = "gamer"..player.UserId
local data = {
	cash = player.PlayerData.Cash.Value,
	carsOwned = {},
}
for i,carsInFolder in pairs(player.OwnedCars:GetChildren()) do
	table.insert(data.carsOwned,carsInFolder.Name)
     end
    local success, err = pcall(function()
	DataStore:SetAsync(key,data)
    end)
  if success then
	print(player.Name.."'s Data Has Saved")
end
if err then
	warn("Failed To Save For "..player.Name)
end
 end

game.Players.PlayerAdded:Connect(Load)
game.Players.PlayerRemoving:Connect(Save)

You can load a table with one key. I edited you code a bit so it loads data correct. If you want me to explain more just lmk.

local DatastoreService = game:GetService("DataStoreService")
local DataStore = DatastoreService:GetDataStore("PlayerData")

local function Load(player)
    local key = "gamer"..player.UserId

    local data

    local success, err = pcall(function()
        data = DataStore:GetAsync(key)
    end)

    if not success then
        warn(err)
        return
    end

    if not data then
        data = {
            cash = nil,
            carsOwned = {}
        }
    end

    local stats = Instance.new("Folder")
    stats.Name = "PlayerData"
    stats.Parent = player


    local cash = Instance.new("IntValue")
    cash.Name = "Cash"
    cash.Parent = stats

    local cars = Instance.new("Folder")
    cars.Name = "OwnedCars"
    cars.Parent = player

    if data.cash then
        cash.Value = data.cash
    else
        cash.Value = 3000
    end

    for _,carSaved in pairs(data.carsOwned) do
        if cars:FindFirstChild(carSaved) then
            cars[carSaved]:Clone().Parent = player.OwnedCars
        end
    end
end

local function Save(player)
    local key = "gamer"..player.UserId

    local data = {
        cash = player.PlayerData.Cash.Value,
        carsOwned = {},
    }

    for _,carsInFolder in pairs(player.OwnedCars:GetChildren()) do
        table.insert(data.carsOwned,carsInFolder.Name)
    end

    local success, err = pcall(function()
        DataStore:SetAsync(key,data)
    end)

    if success then
        print(player.Name.."'s Data Has Saved")
    end

    if err then
        warn("Failed To Save For "..player.Name)
    end
end

game.Players.PlayerAdded:Connect(Load)
game.Players.PlayerRemoving:Connect(Save)
1 Like

I would really appreciate if u where to explain it since I’m trying to learn it and not just have a working code and not understanding it hehe if u want to add me on discord please do so i would be more then willing to give u a form of payment every now and then for your help since i might be running a few thing by you

Discord: Stylxus#6969