So currently im making vehicle saving, and one of the issues i have ran into is saving vehicles into a table
if Type == "Purchase" then
if Player.leaderstats.Wallet.Value >= VehicleData.VehiclePrice then
local setupSave = function(Player, Vehicle)
local savedData = {
Vehicles = {
Vehicle.Name
}
}
return savedData
end
local Success, Err = pcall(function()
s0urceDatabase:GetAsync(Player.UserId.. "__S0urcebase_DataBaseTESTKEY")
end)
if Success then
s0urceDatabase:SetAsync(Player.UserId.. "__S0urcebase_DataBaseTESTKEY", setupSave(Player, Vehicle))
print(s0urceDatabase:GetAsync(Player.UserId.. "__S0urcebase_DataBaseTESTKEY"))
end
end
end
i tried this but it only saves on vehicle and clears the others.
I would recommend using a module for this instead. You can simply use it to handle every player’s data and easily get it and save it with functions which creates data, adds data and returns data.
-- Example usage of functions
local function GetData(client)
local Data
local Success = pcall(function()
Data = DataStore:GetAsync(client.UserId)
end)
if Success and Data then
local NewData = Module.new(client.UserId)
for _, name in ipairs(Data) do
Module.Add(client.UserId, name)
end
else
Module.new(client.UserId)
end
end
local function AddVehicle(client, vehiclename)
Module.Add(client.UserId, vehiclename)
end
local function SaveData(client)
local Data = Module.Get(client.UserId)
pcall(function()
DataStore:SetAsync(client.UserId, Data)
end)
end
-- Module
local Data = { }
function Data.new(id)
Data[id] = { }
return Data[id]
end
function Data.Add(id, vehiclename)
table.insert(Data[id], vehiclename)
end
function Data.Get(id)
return Data[id]
end
return Data