Trouble with DataStore

I can’t figure out why this script won’t work.
Can someone help me?

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MyDataStore")

game.Players.PlayerRemoving:Connect(function(Player)
	local DataToSave = {
		Player.leaderstats.Money.Value,
		Player.OwnedCars.CopCar.Value
	}
	local Data
	local success, err = pcall(function()
		DataToSave:SetAsync(Player.UserId, DataToSave)
	end)
	if success then
		print("Saved")
	else
		print("Not saved!")
		warn(err)
	end
	
end)

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	local Money = Instance.new("IntValue")
	Money.Parent = leaderstats
	Money.Name = "Money"
	local OwnedCars = Instance.new("Folder")
	OwnedCars.Parent = Player
	OwnedCars.Name = "OwnedCars"
	local SpawnedCars = Instance.new("StringValue")
	SpawnedCars.Parent = Player
	SpawnedCars.Name = "SpawnedCars"
	SpawnedCars.Value = "NoCarSpawned"
	local CopCar = Instance.new("StringValue")
	CopCar.Parent = OwnedCars
	CopCar.Name = "CopCar"
	CopCar.Value = "NotOwned"
	local Data
	local success, err = pcall(function()
		Data = DataStore:GetAsync(Player.UserId)
	end)
	if success and Data then
		CopCar.Value = Data[1]
		Money.Value = Data[2]
		print("Data loaded")
		print(Data[2])
	else
		print("No data found!")
	end
end)

Output:

1 Like

On the line where you are saving data, you wrote DataToSave, not DataStore

1 Like

I don’t know why I didn’t notice that, thanks!

1 Like