CantStoreValue problem

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

The screenshot of recently system
image

1 Like

Try this

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



1 Like

This make a new error
Code

game.Players.PlayerRemoving:Connect(function(plr)
	globalfunctions.Save(plr)
end)

Error

  17:22:53.537  ServerScriptService.Game.Game:30: attempt to call a nil value  -  Server - Game:30
1 Like

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)

1 Like

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))
1 Like

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

1 Like

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.

1 Like

I wouldn’t mind getting an explanation so I can do it myself

1 Like

I fix this variant of solve my problem but it still don’t save a dictionarie

1 Like

How to up this topic to get answer?

I think the reason this is happening is due to you using a numeric key that doesn’t start at 1 here:
image

Can you confirm if you’re saving the entire table in your screenshot, with this numeric key included?

1 Like

In that script I only save [uid] = {}

Yeah that would be your issue. Do you need to be using the user id as an index? If you do, you could try tostringing it

1 Like

I solve my problem
I just Encode all info in JSON using HttpService

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