Values table not saving

game.Players.PlayerRemoving:Connect(function(plr)
	
	local key = plr.UserId.."-CARSAVE"

	local datastore =  game:GetService("DataStoreService"):GetDataStore("CAR_DATASTORE")

	local datatable = {}
	local tablenum = 0

	local folder = plr:WaitForChild("OwnedCarros")
	
	for _,value in pairs(folder:GetChildren()) do
		tablenum = tablenum + 1
		table.insert(datatable,tablenum,{["Value"] = value.Value;["Name"] = value.Name})
	end

	local success, err = pcall(function()
		datastore:UpdateAsync(key, function(old)
			local new = old or nil
			new = datatable
			return new
		end)
	end)
	
end)

game.Players.PlayerAdded:Connect(function(plr)

	local key = plr.UserId.."-CARSAVE"

	local datastore =  game:GetService("DataStoreService"):GetDataStore("CAR_DATASTORE")
	
	local datatable = {}
	local tablenum = 0

	local folder = plr:WaitForChild("OwnedCarros")

	for _,value in pairs(folder:GetChildren()) do
		tablenum = tablenum + 1
		table.insert(datatable,tablenum,{["Value"] = value.Value;["Name"] = value.Name})
	end

	local success, err = pcall(function()
		datastore:UpdateAsync(key, function(old)
			local new = old or nil
			new = datatable
			return new
		end)
	end)
	
	datatable = datastore:GetAsync(plr.userId.."-CARSAVE")
	
	if datatable then

		local folder = plr:WaitForChild("OwnedCarros")
		local tablenum = 0

		for _,value in pairs(folder:GetChildren()) do
			local data = nil
			for i = 1, #datatable do
				local check = datatable[i]
				local name = check["Name"]
				if name and value.Name == name then
					data = check
				end
			end
			if data ~= nil then
				if value.Name == data.Name then
					value.Value = data.Value
				end
			end
		end
	end
	
	
end)

Idk what’s wrong with it

Try this:

--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--//Variables
local CarDataStore = DataStoreService:GetDataStore("CAR_DATASTORE")

--//Functions
Players.PlayerAdded:Connect(function(player)
	local key = player.UserId.. "-CARSAVE"
	local datatable = CarDataStore:GetAsync(player.userId.. "-CARSAVE")

	if datatable then
		local folder = player:WaitForChild("OwnedCarros")

		for _, value in ipairs(folder:GetChildren()) do
			local data = nil

			for i = 1, #datatable do
				local check = datatable[i]
				local name = check.Name

				if name and value.Name == name then
					data = check
				end
			end

			if not data and value.Name == data.Name then
				value.Value = data.Value
			end
		end
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local key = player.UserId.. "-CARSAVE"

	local datatable = {}

	local folder = player:WaitForChild("OwnedCarros")

	for _, value in ipairs(folder:GetChildren()) do		
		table.insert(datatable, {
			Value = value.Value, 
			Name = value.Name
		})
	end

	local success, err = pcall(function()
		CarDataStore:SetAsync(key, datatable)
	end)

	if not success then
		warn(err)
	end
end)

Seems like you forgot to remove the saving function inside your PlayerAdded function.

Doesn’t work, no errors at all

Try this, maybe you have to add some values.

--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--//Variables
local CarDataStore = DataStoreService:GetDataStore("CAR_DATASTORE")

--//Functions
Players.PlayerAdded:Connect(function(player)
	local key = player.UserId.. "-CARSAVE"
	local datatable = CarDataStore:GetAsync(player.userId.. "-CARSAVE")

	if datatable then
		local folder = player:WaitForChild("OwnedCarros")

		for _, value in ipairs(datatable) do
			local newValue = Instance.new("StringValue")
			newValue.Name = value.Name
			newValue.Value = value.Value
			newValue.Parent = folder
		end
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local key = player.UserId.. "-CARSAVE"

	local datatable = {}

	local folder = player:WaitForChild("OwnedCarros")

	for _, value in ipairs(folder:GetChildren()) do		
		table.insert(datatable, {
			Value = value.Value, 
			Name = value.Name
		})
	end

	local success, err = pcall(function()
		CarDataStore:SetAsync(key, datatable)
	end)

	if not success then
		warn(err)
	end
end)

image

The values already get added by another script, i just need to save the booleans in the folder
But it’s not working

Ohhh, try this then:

--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--//Variables
local CarDataStore = DataStoreService:GetDataStore("CAR_DATASTORE")

--//Functions
Players.PlayerAdded:Connect(function(player)
	local key = player.UserId.. "-CARSAVE"
	local datatable = CarDataStore:GetAsync(player.userId.. "-CARSAVE")

	if datatable then
		local folder = player:WaitForChild("OwnedCarros")

		for _, value in ipairs(datatable) do
			task.spawn(function()
				local boolValue = folder:WaitForChild(value.Name)
				boolValue.Value = value.Value
			end)
		end
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local key = player.UserId.. "-CARSAVE"

	local datatable = {}

	local folder = player:WaitForChild("OwnedCarros")

	for _, value in ipairs(folder:GetChildren()) do		
		table.insert(datatable, {
			Value = value.Value, 
			Name = value.Name
		})
	end

	local success, err = pcall(function()
		CarDataStore:SetAsync(key, datatable)
	end)

	if not success then
		warn(err)
	end
end)
1 Like

Ohh it worked, i see now, thank you so much!!

No problem. If you have any more questions, feel free to ask.