Datastore does not work, zero clue how to fix

I have tried at least 5 different scripts for my data store.

All of them have printed “success” in the output, yet I never get any of my in-game data saved.
I always BindToClose() at the end to make sure it works, and I’ve tried publishing my game and testing it there, yet it still fails to save data.

What am I doing wrong? Has anyone had a similar issue and know the steps to fix?


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

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
	points.Value = 0
	
	local PlayerID = "PlayerID_"..player.userId
	local success, result = pcall(function()
		return DataStore:GetAsync(PlayerID)
	end)

	if result then
		points.Value = result
	end
end)

game.Players.PlayerRemoving:Connect(function(player)

	local PlayerID = "PlayerID_"..player.UserId
	local PointsData = player.leaderstats.Points.Value


	local Success, Error = pcall(function()
		DataStore:SetAsync(PlayerID, PointsData)
	end)

	if Success then
		print("Data saved successfully.")
	else
		print("Error: Data not saved.")
		warn(Error)
	end
end)

game:BindToClose(function()
	wait(2)
end)

Do u have the ** Enable Studio Access to API Services settings on?

Try

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do 
		local PlayerID = "PlayerID_"..player.UserId
		local PointsData = player.leaderstats.Points.Value
		
		local success, errormsg  = pcall(function()
			DataStore:SetAsync(PlayerID, PointsData)
		end)
		
		if success then
			print("Data saved sucessfully")
		else
			warn(errormsg)
		end	
	end	
end)

I got an error saying there were too many requests and DataStore couldn’t process them at the same time.

It seems like the saving doesn’t work for points that are gained during a play session, the data saving only works if I set it to a number in the script.

Any thoughts

Yeah I do, I enabled that a while ago so that wouldn’t be the issue. Like I said there aren’t any errors in the output

This is because studio triggered both the bind to close event and player removing event.

Should I remove one of the events to make it work?

No, that’s just how it is in studio. In a real game it shouldn’t error.

Try this:


local Players = game:GetService("Players")
local TestService = game:GetService("TestService")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("DataStore1") --Name the DataStore whatever you want

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.Value = 0
    Points.Parent = leaderstats
 

	local value1Data = Points


	local s, e = pcall(function()
		value1Data = myDataStore:GetAsync(player.UserId..'-Value1') or 0 --check if they have data, if not it'll be "0"
	end)

	if s then
		Points.Value = value1Data --setting data if its success
	else
		TestService:Error(e)  --if not success then we error it to the console
	end
end)

Players.PlayerRemoving:Connect(function(player)
local s, e = pcall(function()
	myDataStore:SetAsync(player.UserId..'-Value1', player.leaderstats.Points.Value) --setting data
	end)
	if not s then TestService:Error(e) 
	end
end)

game:BindToClose(function()
local s, e = pcall(function()
	for _, player in ipairs(Players:GetPlayers()) do
		myDataStore:SetAsync(player.UserId..'-data', player.leaderstats.Points.Value)
	end
  end)
      if not s then TestService:Error(e)
        end
end)

Fixed it, just realized I had the data being added in a LocalScript, I didn’t know that the data only updates on server scripts. I used a remote event to fix it.

Thanks for the replies!

1 Like