Datastore not saving

Heres the script:

local ds = game:GetService("DataStoreService"):GetDataStore("carsave")

game.Players.PlayerAdded:Connect(function(player)
	local data = Instance.new("Folder", player)
	data.Name = "cars"
	
	
	local corvan = Instance.new("IntValue", data)
	corvan.Name = "oldvan"
	local corvancolor = Instance.new("IntValue", data)
	corvancolor.Name = "oldvancolor"
	
	local vans = Instance.new("IntValue", data)
	vans.Name = "van"
	local vancolor = Instance.new("IntValue", data)
	vancolor.Name = "vancolor"
	
	
	local sedan = Instance.new("IntValue", data)
	sedan.Name = "sedan"
	local sedancolor = Instance.new("IntValue", data)
	sedancolor.Name = "sedancolor"
	
	
	local musclecar = Instance.new("IntValue", data)
	musclecar.Name = "musclecar"
	local musclecarcolor = Instance.new("IntValue", data)
	musclecarcolor.Name = "musclecarcolor"
	
	
	local suv = Instance.new("IntValue", data)
	suv.Name = "suv"
	local suvcolor = Instance.new("IntValue", data)
	suvcolor.Name = "suvcolor"
	
	
	local sportscar = Instance.new("IntValue", data)
	sportscar.Name = "sportscar"
	local sportscarcolor = Instance.new("IntValue", data)
	sportscarcolor.Name = "sportscarcolor"
	
	
	local supercar = Instance.new("IntValue", data)
	supercar.Name = "supercar"
	local supercarcolor = Instance.new("IntValue", data)
	supercarcolor.Name = "supercarcolor"
	
	
	local truck = Instance.new("IntValue", data)
	truck.Name = "truck"
	local truckcolor = Instance.new("IntValue", data)
	truckcolor.Name = "truckcolor"
	
	
	local lightutilityvehicle = Instance.new("IntValue", data)
	lightutilityvehicle.Name = "lightutilityvehicle"
	local lightutilityvehiclecolor = Instance.new("IntValue", data)
	lightutilityvehiclecolor.Name = "lightutilityvehiclecolor"
	
	
	local dunebuggy = Instance.new("IntValue", data)
	dunebuggy.Name = "dunebuggy"
	local dunebuggycolor = Instance.new("IntValue", data)
	dunebuggycolor.Name = "dunebuggycolor"
	
	
	local stats = ds:GetAsync(player.UserId)
	
	if stats ~= nil then
		corvan.Value = stats[1]
		vans.Value = stats[3]
		sedan.Value = stats[5]
		musclecar.Value = stats[7]
		suv.Value = stats[9]
		sportscar.Value = stats[11]
		supercar.Value = stats[13]
		truck.Value = stats[15]
		lightutilityvehicle.Value = stats[17]
		dunebuggy.Value = stats[19]
		
		corvancolor.Value = stats[2]
		vancolor.Value = stats[4]
		sedancolor.Value = stats[6]
		musclecarcolor.Value = stats[8]
		suvcolor.Value = stats[10]
		sportscarcolor.Value = stats[12]
		truckcolor.Value = stats[14]
		lightutilityvehiclecolor.Value = stats[16]
		dunebuggycolor.Value = stats[18]
		supercarcolor.Value = stats[20]
	else
		corvan.Value = 0
		vans.Value = 0
		sedan.Value = 0
		musclecar.Value = 0
		suv.Value = 0
		sportscar.Value = 0
		truck.Value = 0
		lightutilityvehicle.Value = 0
		dunebuggy.Value = 0
		
		corvancolor.Value = 1
		vancolor.Value = 1
		sedancolor.Value = 1
		musclecarcolor.Value = 1
		suvcolor.Value = 1
		sportscarcolor.Value = 1
		truckcolor.Value = 1
		lightutilityvehiclecolor.Value = 1
		dunebuggycolor.Value = 1
		supercarcolor.Value = 1
		
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local save = {}
	
	table.insert(save, player.cars.oldvan.Value)
	table.insert(save, player.cars.van.Value)
	table.insert(save, player.cars.sedan.Value)
	table.insert(save, player.cars.suv.Value)
	table.insert(save, player.cars.sportscar.Value)
	table.insert(save, player.cars.truck.Value)
	table.insert(save, player.cars.lightutilityvehicle.Value)
	table.insert(save, player.cars.dunebuggy.Value)
	
	table.insert(save, player.cars.oldvancolor.Value)
	table.insert(save, player.cars.vancolor.Value)
	table.insert(save, player.cars.sedancolor.Value)
	table.insert(save, player.cars.suvcolor.Value)
	table.insert(save, player.cars.sportscarcolor.Value)
	table.insert(save, player.cars.truckcolor.Value)
	table.insert(save, player.cars.lightutilityvehiclecolor.Value)
	table.insert(save, player.cars.dunebuggycolor.Value)
	
	ds:SetAsync(player.UserId, save)
end)

I am worried that this is too much info to save, because every now and then when I buy an item, leave and rejoin, it doesn’t save. I looked somewhere on here and it said that datastores can have 40 set/gets, this is twenty. Is this too much? Or is there any other way to save this, that is the color of the car (using int values, for example if the vancolor int value was 1, itd be purple for example. same with cars, 1 = owned, 0 = not owned)

Is studio access to datastore service enabled?

PlayerRemoving frequently doesn’t fire in studio so if you want to test the DataStore saving/loading while in studio use the following.

https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

1 Like

Yes, I forget to menton that about half of the values save, the others dont

Does it fire in a regular game? I don’t care about my data in studio as I could just give myself money, I don’t allow other players to do that. Hence why I made this topic.

Yes, it fires in a regular session. Saving everything inside a single table value is fine, issuing too many DataStore requests will cause data-loss (queued save/load requests will be dropped).

1 Like

alright, thanks. just got a bit worried.