Datastore not saving at all

I don’t know why but I have always have problems with datastores. I decided to go back to basics with datastores.

This is my script and it doesn’t work at all:

local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("TestData")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Parent = leaderstats
	
	local playerUserId = "Player_"..player.UserId
	
	-- Load Data
	
	local success, errormessage = pcall(function()
		data = TestDataStore:GetAsync(playerUserId)
	end)
	print(data)--prints 0
	
	if success then
		print(data) -- prints 0
		Points.Value = data
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	
	local data = player.leaderstats.Points.Value
	print(data) -- prints the correct value
	
	local success, errormessage = pcall(function()
		print("Saving data")
		TestDataStore:SetAsync(playerUserId, data)
	end)
	
	
	if success then
		print("Data successfully saved")
	else
		print("There was a error when saving data")
		warn(errormessage)
	end

end)

Why doesn’t it work?

Sometimes datastores dont work properly in studio, try it in game?

1 Like

data isnt a variable so thats why its erroring maybe