Why doesn't my Datastore save the data,?

So i just finished the gnome code’s video on how to make a datastore and it doesn’t work, I’m kinda clueless right now because i don’t even have any errors or anything like that. I have the datastore enabled in game settings, but still nothing.

local Players = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")

local database = DatastoreService:GetDataStore("Datastore")
local sessionData = {}

function playerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	local Cash = Instance.new("NumberValue", leaderstats)
	Cash.Name = "Cash"
	
	local Gems = Instance.new("NumberValue", leaderstats)
	Gems.Name = "Gems"
	
	local success = nil
	local playerData = nil
	local attempt = 1
	
	local UserId = player.UserId
	
	repeat
	success, playerData = pcall(function()
		return database:GetAsync(UserId)
	end)

	attempt += 1
	if not success then
		print(playerData)
		task.wait(3)
	end
	until success or attempt == 5
	
	if success then
		print("Connected to database")
		
		if not playerData then
			print("Assigning default data")
			playerData = {
				["Cash"] = 15;
				["Gems"] = 0;
				["Pets"] = {
					"Cat"
				}
			}
		end
		sessionData[UserId] = playerData
	else
		warn("Unable to get data for "..UserId)
		player:Kick("Unable to load your data. Try again later.")
	end
	
	Cash.Value = sessionData[UserId].Cash
	Gems.Value = sessionData[UserId].Gems
	
	Cash.Changed:Connect(function()
		sessionData[UserId].Cash = Cash.Value
	end)
	Gems.Changed:Connect(function()
		sessionData[UserId].Gems = Gems.Value
	end)
	
	leaderstats.Parent = player
end

Players.PlayerAdded:Connect(playerAdded)


----------------------------------------------------------------
function playerLeaving(player)
	local UserId = player.UserId
	
	if sessionData[UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1
		
		repeat
			success, errorMsg = pcall(function()
				database:SetAsync(UserId, sessionData[UserId])
			end)
			
			attempt += 1
			if not success then
				print(errorMsg)
				task.wait(3)
			end
		until success or attempt == 5
		
		if success then
			print("Data saved for "..player.Name)
		else
			warn("Unable to save for "..player.Name)
		end
	end
end

Players.PlayerRemoving:Connect(playerLeaving)

function serverShutdown(player)
	for i, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			playerLeaving(player)
		end)
	end
end

game:BindToClose(serverShutdown)

What doesn’t work? The joining or the saving data?

Saving the data. Also when i join i don’t start with 15 cash and when i change my cash value using command it doesn’t save.

I see that the leaderstats folder isn parented to the player after getting the data, it should be at the top of the code and the cash and gems should be intvalues, not numbervalues.

Roblox datastore is known for having data loss, you might want to see if the script saves the data or returns any error.
Thankfully there are a bunch of fixes like ProfileService and Datastore V2 Both work very well. You can try making your own perfect data store system by taking some inspiration from these 2 amazing methods.

I tried setting them to int values but nothing. Also i set the parent at the end of the player added function. I tried setting it earlier but still nothing

I don’t really see the point of the sessiondata table. In the player leaving function, you can just create a table called PlayerData. Smth like this (the index has to be the same name as the values in the leaderstats)

local PlayerData = {
          [“Cash”] = player.leaderstats.Cash.Value
          [“Gems”] = player.leaderstats.Gems.Value
}

Then in the playeradded function you can just iterate through the table you get like this (assign the table to a variable in the pcall first and check if player even has data)

for i,v in pairs(PlayerData) do
        if leaderstats:FindFirstChild(i) then
               leaderstats[i].Value = v
        end
end

Hey I’ve tested your script and it gets the job done, make sure you got Enable Studio Access To API Services.

How to enable it:

  1. Click “FILE” on the top-left of your screen
  2. Find “Game Settings”
  3. Head to “Security”
  4. Tick the 3rd Option “Enable Studio Access To API Services” and make sure it turns green

Keep in mind:

  1. It has to be a server-side script placed in ServerScriptService that you can find on your explorer
  2. The value can only be changed from the server, going into the explorer and editing the value will NOT work because you’re doing it from the client
  3. To edit the value from the client you should call a remote event

If none of these options works you should add some checkpoints with the function
Example:

Print("Loading Data Cash")

If the checkpoints are all regular and the script seems to be going as it should check for some other scripts that might be interfering with the handling of the values that should be saved

Taking a look at the Roblox Official DataStore Service won’t hurt as well.

He did say in the topic post that he enabled API services

1 Like