DataStore: Data not being saved

Hi.
I began reworking my DataStore system, because the old one was using a trillion :GetAsync()'s, i wanted to make my DataStore run more efficient.
So i began with the help of a few posts, changing my DataStore script.

First of all, if I join the game, I do not even get the values i’ve set for the players who join for the first time.
Second, if I switch to Server and change one of my values and leave the game, they do not get applied. The output does not show any single error/success message

At first, I had an issue where I had an error with arrays not being supported by the DataStore. So i tried with JSON-Encoding, it resulted me giving a Success message, but it didn’t save my stats. I then realized that there are BrickColor values that the DataStore array might not support so I changed them to IntValues. Since that, nothing worked anymore.

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("fbgdata")
local HttpService = game:GetService("HttpService")


game.Players.PlayerAdded:connect(function(Player)
	local leaderstats = Instance.new('Folder', Player)	
	leaderstats.Name = 'leaderstats'
	local distance = Instance.new('IntValue', leaderstats)
	distance.Name = 'Kilometer'
	local money = Instance.new('IntValue', leaderstats)
	money.Name = "Euro"
	local values = Instance.new("Folder", Player)
	values.Name = "Values"	
	local c1 = Instance.new("IntValue", Player)
	c1.Name = "car1"
	local c2 = Instance.new("IntValue", Player)
	c2.Name = "car2"
	local bewusstlos = Instance.new("IntValue", Player)
	bewusstlos.Name = "bewusstlos"
	local lp = Instance.new("Folder", Player.Values)	
	lp.Name = "licenseplate"
	local b = Instance.new("StringValue", Player.Values.licenseplate)	
	b.Name = "b"
	local n = Instance.new("IntValue", Player.Values.licenseplate)
	n.Name = "n"
	local garage = Instance.new("Folder", Player)	
	garage.Name = "garage"
	local car1 = Instance.new("IntValue", Player.garage)	
	car1.Name = "car1"
	local car1a = Instance.new("StringValue", Player.garage.car1)	
	car1a.Name = "trim"	
	local car1b = Instance.new("IntValue", Player.garage.car1)
	car1b.Name = "color"
	local car2 = Instance.new("IntValue", Player.garage)
	car2.Name = "car2"
	local car2a = Instance.new("StringValue", Player.garage.car2)
	car2a.Name = "trim"
	local car2b = Instance.new("IntValue", Player.garage.car2)
	car2b.Name = "color"
	local car3 = Instance.new("IntValue", Player.garage)
	car3.Name = "car3"
	local car3a = Instance.new("StringValue", Player.garage.car3)
	car3a.Name = "trim"
	local car3b = Instance.new("IntValue", Player.garage.car3)
	car3b.Name = "color"	
	local car4 = Instance.new("IntValue", Player.garage)
	car4.Name = "car4"
	local car4a = Instance.new("StringValue", Player.garage.car4)
	car4a.Name = "trim"
	local car4b = Instance.new("IntValue", Player.garage.car4)
	car4b.Name = "color"
	local spawnedcars = Instance.new("IntValue", Player.garage)
	spawnedcars.Name = "spawnedcars"
	local ghljob = Instance.new("Folder", Player.Values)
	ghljob.Name = "ghljob"
	local ghlveh = Instance.new("Folder", Player.Values.ghljob)	
	ghlveh.Name = "ghlvehicles"
	local ghlxp = Instance.new("NumberValue", Player.Values.ghljob)
	ghlxp.Name = "xp"
	local conjob = Instance.new("Folder", Player.Values)
	conjob.Name = "conjob"
	local conveh = Instance.new("Folder", Player.Values.conjob)
	conveh.Name = "convehicles"
	local conxp = Instance.new("NumberValue", Player.Values.conjob)
	conxp.Name = "xp"
	local setting = Instance.new("Folder", Player.Values)
	setting.Name = "settings"
	local rend = Instance.new("NumberValue", Player.Values.settings)
	rend.Name = "renddist"
	
	local toSave

	local success, err = pcall(function()
		toSave = myDataStore:GetAsync("data" ..Player.UserId)
	end)
	
	if success and toSave then
		if type(toSave) == "table" then
			money.Value = toSave[1] or 2000
			distance.Value = toSave[2] or 5
			b.Value = toSave[3] or string.char(math.random(65,90)) .. string.char(math.random(65,90))
			n.Value = toSave[4] or math.random(1,99)
			ghlxp.Value = toSave[5] or 0
			conxp.Value = toSave[6] or 0
			rend.Value = toSave[7] or 2000
			car1.Value = toSave[8] or 0
			car1a.Value = toSave[9] or "none"
			car1b.Value = toSave[10] or 1
		
			car2.Value = toSave[11] or 0
			car2a.Value = toSave[12] or "none"
			car2b.Value = toSave[13] or 1
		
			car3.Value = toSave[14] or 0
			car3a.Value = toSave[15] or "none"
			car3b.Value = toSave[16] or 1
		
			car4.Value = toSave[17] or 0
			car4a.Value = toSave[18] or "none"
			car4b.Value = toSave[19] or 1
			
			rend.Value = toSave[20] or 2000
		end
	else
		warn(err)
	end
	
	local toSaveData = {money.Value, distance.Value, b.Value, n.Value, ghlxp.Value, conxp.Value, rend.Value, car1.Value, car1a.Value, car1b.Value, car2.Value, car2a.Value, car2b.Value, car3.Value, car3a.Value, car3b.Value, car4.Value, car4a.Value, car4b.Value, rend.Value}

	game.Players.PlayerRemoving:Connect(function(Player)
		local success, err = pcall(function()
			myDataStore:SetAsync("data" ..Player.UserId, toSaveData)
			if not success then
				warn(err)
			else
				print("success")
			end

		end)
		
	end)
end)

(I know this is not the best way to save data for cars, but I limited the ownable cars to 4 so I’ve made 4 slots)

Edit: Ignore the unused ghlvehicles and convehicles variables, they’ll be used in future

2 Likes

This needs to be placed outside the pcall. Also, why is the player removing event inside the player added event?