Save to datastore failed

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Recording value of value

  1. What is the issue? Include screenshots / videos if possible!

value value is not saved without any error

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

no

local datastoreS = game:GetService("DataStoreService")
local store = datastoreS:GetDataStore("StandId")

game:GetService("Players").PlayerAdded:Connect(function(plr)
	local fold = Instance.new("Folder",plr)
	fold.Name = "Stand"
	local first = store:GetAsync(plr.UserId) or 0
	local val = Instance.new("IntValue",fold)
	val.Name = "Stand"
	val.Value = first
	print(plr.Stand.Stand.Value)
end)
game.Players.PlayerRemoving:Connect(function(plr)
	store:SetAsync(plr.UserId, plr.Stand.Stand.Value)
	print(plr.Stand.Stand.Value)
end)
1 Like

Warp the :GetAsync in a pcall() and print out the error. Then we can proceed.

1 Like

First of all I see many things that could cause this.

  1. You didn’t wrap the async functions in a pcall.
  2. Use game:BindToClose() to save the players data when the server is shutting down
  3. Studio often closes too quickly for the datastores to save so if you don’t wanna keep it open longer manually when you close the studio session, try it out in the real game. If you do want to, look at 2. and save each player alongside each other with a coroutine so that the time is sufficient enough to save each player.
1 Like

Here is an example of the pcall

Try this

local DSS = game:GetService("DataStoreService")
local valueStore = DSS:GetDataStore("valueStore")

game.Players.PlayerAdded:Connect(function(plr)
  local fold = Instance.new("Folder",plr)
  fold.Name = "Stand"
  local val = Instanc.new("IntValue",fold)
  val.Name = "Stand"
  local first   -- adding a nil value which will be our value
  local success, errorMessage = pcall(function()
    first = valueStore:GetAsync(plr.UserId) 
    
  end
  if success then
    val.Value = first
  end

  if errorMessage then
     warn(errorMessage) -- Gives the error then
  end

end)



 game.Players.PlayerRemoving:Connect(function(plr)
      local sucess, errMessage = pcall(function()
              valueStore:SetAsync(plr.UserId,plr.Stand.Value)

      end

      if sucess then
          print("Data has been saved!")
      end

      if errMessage then
         warn(errMessage)
     end

 end

Make sure to enable API servicex as well!

1 Like

First of all, thanks to everyone, the problem was caused by roblox studio quitting the fast game.

local datastoreS = game:GetService("DataStoreService")
local store = datastoreS:GetDataStore("StandId")

game:GetService("Players").PlayerAdded:Connect(function(plr)
	local fold = Instance.new("Folder",plr)
	fold.Name = "Stand"
	local first = store:GetAsync(plr.UserId) or 0
	local val = Instance.new("IntValue",fold)
	val.Name = "Stand"
	val.Value = first
	print(plr.Stand.Stand.Value)
end)
game.Players.PlayerRemoving:Connect(function(plr)
	pcall(function()
		store:SetAsync(plr.UserId, plr.Stand.Stand.Value)
	end)
	print(plr.Stand.Stand.Value)
end)
game:BindToClose(function()
    for i, v in pairs(game.Players:GetPlayers()) do
        pcall(function()
            store:SetAsync(v.UserId, v.Stand.Stand.Value)
        end)
    end
end)
1 Like