The problem
Hello. I was making a car saving system and ran into a problem. Datastore doesn’t want to save the dictionary. Then I wondered how I recently made a save system? And I went to the site with that system and the data storage there saved the dictionary and did not complain (attached a screenshot). Please help me, I can’t think with my head at all.
The code
function globalfunctions.Save(plr:Player)
local Cars = {}
local Data = {
["Cash"] = plr.leaderstats["Рублей"].Value,
["Level"] = plr.leaderstats["Уровень"].Value,
["Card"] = plr.DataFolder.Card.Value
}
for i,v in plr:FindFirstChild('PlayerCars'):GetChildren() do
local CarName = v.Value
local Color = v:GetAttribute("Color")
local datacar = {
["Car"] = CarName,
["Color"] = Color
}
Cars[i] = datacar
end
print(Data)
print(Cars)
DataStore:SetAsync(tostring(plr.UserId),Data)
DataStore:SetAsync(tostring(plr.UserId).."Cars",Cars)
print("Данные сохранены")
end
local function Save(plr: Player)
local Cars = {}
local Data = {
["Cash"] = plr.leaderstats["Рублей"].Value,
["Level"] = plr.leaderstats["Уровень"].Value,
["Card"] = plr.DataFolder.Card.Value
}
for _, v in pairs(plr:FindFirstChild('PlayerCars'):GetChildren()) do
local CarName = v.Value
local Color = v:GetAttribute("Color")
local datacar = {
["Car"] = CarName,
["Color"] = Color
}
Cars[_] = datacar
end
print(Data)
print(Cars)
local success, errorMessage = pcall(function()
DataStore:SetAsync(tostring(plr.UserId), Data)
DataStore:SetAsync(tostring(plr.UserId) .. "Cars", Cars)
end)
if success then
print("Data Saved")
else
print("Data Not Saved: " .. errorMessage)
end
end
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("YourDataStoreName") -- Initialize your DataStore
local function Save(plr: Player)
local Cars = {}
local Data = {
["Cash"] = plr.leaderstats and plr.leaderstats["Рублей"] and plr.leaderstats["Рублей"].Value or 0,
["Level"] = plr.leaderstats and plr.leaderstats["Уровень"] and plr.leaderstats["Уровень"].Value or 0,
["Card"] = plr.DataFolder and plr.DataFolder.Card and plr.DataFolder.Card.Value or nil,
}
for _, v in pairs(plr:FindFirstChild('PlayerCars'):GetChildren()) do
local CarName = v.Value
local Color = v:GetAttribute("Color")
local datacar = {
["Car"] = CarName,
["Color"] = Color
}
table.insert(Cars, datacar) -- Use table.insert for proper indexing
end
print("Player Data:", Data)
print("Cars Data:", Cars)
local success, errorMessage = pcall(function()
DataStore:SetAsync(tostring(plr.UserId), Data)
DataStore:SetAsync(tostring(plr.UserId) .. "Cars", Cars)
end)
if success then
print("Data Saved")
else
print("Data Not Saved: " .. errorMessage)
end
end
Players.PlayerRemoving:Connect(function(plr)
Save(plr)
end)
you can use HTTP service to serialize and unserialize your data
local data = {
["a"] = 2,
["b"] = 4,
["c"] = true
}
whateverStore:SetAsync(whateverKey, game:GetService("HttpService"):JSONEncode(data))
-- and then
local data = game:GetService("HttpService"):JSONDecode(whateverStore:GetAsync(whateverKey))
This dont save a cars when i buy it. I try to explain
When a player buy a car in folder that parent is player appear a new stringvalue that have a value - carname and have a attribute named a color where i store car color. So then i save this strngvalue i transform into dictionarie where i store this values
I don’t think it’s a good idea to just give people code, it doesn’t teach them anything and they don’t learn from their mistakes until it happens again.