Something wrong with DataStore

because JSONEncode/Decode isn’t needed:

local storeditems =  game:GetService(‘HTTPService’):JSONDecode(DataStore:GetAsync(key))

can be come just this:

local storeditems = DataStore:GetAsync(key)

and this:

DataStore:SetAsync(key,game:GetService("HTTPService"):JSONEncode(items))

Can Become this:

DataStore:SetAsync(key, items)

From primarily looking at the above code that should fix the errors, but i would like to recommend some improvements in general, firstly i would suggest using pcalls and using UpdateAsync (there are a couple threads that go into detail why and which one you should use), but for now here is my example of using Update Async wrapped in a pcall:

local Success, Error = pcall(function() --- returns success and,  err if there is one 
   DataStore:UpdateAsync(key, function(prev) -- prev is previously saved data
        return items ---saves new data
   end)
end)
1 Like

You would also want to know what will happen if you have success or Error. So:

if success then
print(“Data Saved”)
else
warn(Error)
print(“Data has not been saved”)

1 Like

I think so, try and see what the output will say. I think Jay’s code is better imo.

1 Like

so, we are trying to look in game output or studio output?

Use the studio output whenever something doesn’t work in studio.

1 Like

i’m not sure what you are asking but if you are talking about the pcall, the point of it (in this case) is for in the unlikely event that data isn’t saved/loaded due to a roblox-related issue. Try using without it first, to ensure that the rest of your script isn’t faulty

2 Likes

Also, is your API enabled? API will need to be enabled if you want to save data.

OH MAN, when i clicked play, the values werent 0 like when loading a 0 value, then in studio its like havent loaded the data, will check the game ok?

yes, i have checked this on the other post…

i’m not sure what you mean…is it erroring or something?..could you provide the script you are currently using?

1 Like
-- BY ClanShad27 and THECOOLBOSS99999
local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("DataStore")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(Plr)
	
	local leaderstats = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Plr
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 1
	Level.Parent = leaderstats
	
	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Value = 0
	XP.Parent = leaderstats
	
	local Rank = Instance.new("StringValue")
	Rank.Name = "Rank"
	Rank.Value = "Genin"
	Rank.Parent = leaderstats
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 10
	Coins.Parent = leaderstats
	
	local FC = Instance.new("IntValue")
	FC.Name = "FC"
	FC.Value = 100
	FC.Parent = leaderstats
	
	local key = "user-" .. Plr.userId
	

    local storeditems = DataStore:GetAsync(key)
			
	if storeditems then
		Level.Value = storeditems[1]
		Rank.Value = storeditems[2]
		XP.Value = storeditems[3]
		Coins.Value = storeditems[4]
		FC.Value = storeditems[5]
	else
		local items = {Level.Value, Rank.Value, XP.Value, FC.Value, Coins.Value} --Change Points or Wins if you changed line 10 or 14
		DataStore:SetAsync(key, items)
	end
	
    while wait() do
		wait(.01)
		if XP.Value >= (100 * (Level.Value + 1)) then
			Level.Value = Level.Value + 1
			XP.Value = 0
			game.ReplicatedStorage.LevelUpGui:FireClient(Plr)
		end
	end	
end)

game.ReplicatedStorage.AddXP.OnServerEvent:Connect(function(plr)
	plr.leaderstats.XP.Value = plr.leaderstats.XP.Value + 50
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Level.Value, player.leaderstats.Rank.Value, player.leaderstats.XP.Value, player.leaderstats.Coins.Value, player.leaderstats.FC.Value} --Change Points or Wins if you changed line 10 or 14
	local key = "user-" .. player.userId
	
	DataStore:SetAsync(key, items)
end)

it didnt work, I’m almost crying…
i played on the game then i added xp, leveled up to 2 and when i re opened the game my status was reseted or something like

Well, this is where you need to start debugging , to find out where the problem is occuring. Try adding in some prints where your Player Removing and Player Added events are:

 local storeditems = DataStore:GetAsync(key)
print(storeditems)
print(items)
DataStore:SetAsync(key, items)

From what i can see in your code, everything should work


Also are there any errors in the output that might help you with this debugging process?

No, theres no errors ocurring in the output.

OH I FORGOT/MISS MENTIONING THIS OTHER POST VERY IMPORTANT

What does the print(storeditems) and the print(items) print?

Like this?:

local storeditems = DataStore:GetAsync(key)

print(storeditems)
		
if storeditems then
	Level.Value = storeditems[1]
	Rank.Value = storeditems[2]
	XP.Value = storeditems[3]
	Coins.Value = storeditems[4]
	FC.Value = storeditems[5]
else
	local items = {Level.Value, Rank.Value, XP.Value, FC.Value, Coins.Value} --Change Points or Wins if you changed line 10 or 14
	print(items)
	DataStore:SetAsync(key, items)
end

In line 40, instead of:

local storeditems = local data = game:GetService(‘HTTPService’):JSONDecode(DataStore:GetAsync(key))

Split it into two lines, like this:

local storeditems 
local data = game:GetService(‘HTTPService’):JSONDecode(DataStore:GetAsync(key))
1 Like

the output:
17:20:44.676 - AddXP is not a valid member of ReplicatedStorage

17:20:44.679 - Stack Begin

17:20:44.681 - Script ‘ServerScriptService.leaderstats’, Line 68

17:20:44.682 - Stack End

table: 0xe438bea6a377c972

Would this work?:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Leaderstats") -- Change this with a random name.

game.Players.PlayerAdded:Connect(function(Player)
	
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	
	local XP = Instance.new("StringValue")
	XP.Name = "XP" -- Change this with your XP name.
	XP.Value = DataStore:GetAsync(Player.UserId) or 0
	XP.Parent = Leaderstats
	
	local Level = Instance.new("StringValue")
	Level.Name = "Level" -- Change this with your XP name.
	Level.Value = DataStore:GetAsync(Player.UserId) or 1
	Level.Parent = Leaderstats
	
		local Fc = Instance.new("StringValue")
	Fc.Name = "FC" -- Change this with your XP name.
	Fc.Value = DataStore:GetAsync(Player.UserId) or 100
	Fc.Parent = Leaderstats
	
			local Mny = Instance.new("StringValue")
	Mny.Name = "Coins" -- Change this with your XP name.
	Mny.Value = DataStore:GetAsync(Player.UserId) or 100
	Mny.Parent = Leaderstats
	
	    while wait() do
		wait(.01)
		if XP.Value >= (100 * (Level.Value + 1)) then
			Level.Value = Level.Value + 1
			XP.Value = 0
			game.ReplicatedStorage.LevelUpGui:FireClient(Player)
		end
	end
	
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["XP"] = Player.leaderstats.XP.Value;
		["Level"] = Player.leaderstats.Level.Value;
		["FC"] = Player.leaderstats.FC.Value;
		["Coins"] = Player.leaderstats.Coins.Value;
--		["Level"] = Player.leaderstats.Level.Value;
--		["Level"] = Player.leaderstats.Level.Value;
--		["Level"] = Player.leaderstats.Level.Value;
--		["Level"] = Player.leaderstats.Level.Value;
	})
end)

i’ve played around with you script and found out that the SetAsync is not being fully executed (at least for me) in studio, possibly due to the server shutting down too quickly (which game:BindToClose can “fix”)

Anyways Here is my edited code:

local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("DataStore")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataSaved = {}


Players.PlayerAdded:Connect(function(Plr)
	
	local leaderstats = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Plr
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 1
	Level.Parent = leaderstats
	
	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Value = 0
	XP.Parent = leaderstats
	
	local Rank = Instance.new("StringValue")
	Rank.Name = "Rank"
	Rank.Value = "Genin"
	Rank.Parent = leaderstats
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 10
	Coins.Parent = leaderstats
	
	local FC = Instance.new("IntValue")
	FC.Name = "FC"
	FC.Value = 100
	FC.Parent = leaderstats
	
	local key = "user-"..Plr.userId
	

  local storeditems 
local items = {Level.Value, Rank.Value, XP.Value,Coins.Value, FC.Value}
local success, err = pcall(function()
  storeditems = DataStore:GetAsync(key) or items
end)
	if storeditems then
		Level.Value = storeditems[1]
		Rank.Value = storeditems[2]
		XP.Value = storeditems[3]
		Coins.Value = storeditems[4]
	   FC.Value = storeditems[5]
   end


 while wait() do
		wait(.01)
		if XP.Value >= (100 * (Level.Value + 1)) then
			Level.Value = Level.Value + 1
			XP.Value = 0
			game.ReplicatedStorage.LevelUpGui:FireClient(Player)
		end
	end
end)


game:BindToClose(function()
   for _, plr in ipairs(Players:GetPlayers()) do
	if not DataSaved[plr] then
		local items = {plr.leaderstats.Level.Value, plr.leaderstats.Rank.Value, plr.leaderstats.XP.Value, plr.leaderstats.Coins.Value, plr.leaderstats.FC.Value}
		local key = "user-"..plr.userId
		local success, err = pcall(function()
	       DataStore:UpdateAsync(key, function(prev)
		    return items
	       end)
         end)	
	end
    end
end)


Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Level.Value, player.leaderstats.Rank.Value, player.leaderstats.XP.Value, player.leaderstats.Coins.Value, player.leaderstats.FC.Value} --Change Points or Wins if you changed line 10 or 14
	local key =  "user-"..player.userId
	local success, err = pcall(function()
	DataStore:UpdateAsync(key, function(prev)
		return items
	 end)
       end)	
	  if success then
    DataSaved[player] = true	
  end
end)

Also since i am guessing you want to use this for your game, it’s probably a good idea to add some datastore “retries”. there are also some good articles on the wiki and the devforoum that are useful:


Oh and I am going to just leave this here:

3 Likes