My datastore dosen't work

My datastore doesn’t save the data. I will put the script down below.

local DS = game:GetService("DataStoreService"):GetDataStore("SaveMyData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local savevalue = plr.leaderstats.Money
	local savevalue2 = plr.leaderstats.Level
	
	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then
		savevalue.Value = GetSaved[1]
		savevalue2.Value = GetSaved[2]
		
	else
		local NumbersForSaving = {savevalue.Value}
		DS:GetAsync(plrkey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	DS:SetAsync("id_"..plr.userId, {plr.leaderstats.Cash.Value, plr.leaderstats.Level.Value})
end)

Any solutions?

Can you provide more information? What is it supposed to do? What do you mean that it doesn’t work?

It’s supposed to load the data: money and level. Then i join the game in roblox and after words leave, with higher stats, it dosen’t save the data.

Do you mind telling me what those the output says?

Nothing - no errors ------.-------

1 Like

Well I don’t see that your including any instances like where are you saving these stats too?

I’ve got an error running your scripts saying 12:43:46.362 - leaderstats is not a valid member of Player In the output you should save those stats under a instance folder

Above the datastore script i made this that creates the values:

game.Players.PlayerAdded:connect(function(p)
	
	---- Money ----
	
	local stats = Instance.new("Folder", p)
	stats.Name = "leaderstats"
	local money = Instance.new("IntValue", stats)
	money.Name = "Money"
	money.Value = 0
	
	---- Level ----
	
	local level = Instance.new("IntValue", stats)
	level.Name = "Level"
	level.Value = 1

end)

https://www.youtube.com/watch?v=n1UpT2csAzo this video should help you with your datastore

Made a new version - but now it just says that leaderstats isn’t a valid member og player.

local datastore = game:GetService("DataStoreService"):GetDataStore("GameStore") --Call "GameStore" whatever you like


game.Players.PlayerAdded:connect(function(player)
	local leaderstats = player.leaderstats
	
	local money = leaderstats.Money
	
	local level = leaderstats.Level
	
	local key = "user-" .. player.userId
	
	local storeditems = datastore:GetAsync(key)
	if storeditems then
		money.Value = storeditems[1]
		level.Value = storeditems[2] 
	else
		local items = {money.Value, level.Value} 

		datastore:SetAsync(key, items)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Points.Value, player.leaderstats.Wins.Value}

	local key = "user-" .. player.userId
	
	datastore:SetAsync(key, items)
end)

I am not sure if that’s the problem, but data store requests occasionally fail, so I suggest you to use pcalls(), and only if it succeded to get data, load it.
For saving for example:
and to load:

local data
local success, err = pcall(function()
data = datastore:GetAsync(key)
end)
if(success) then
        money.Value = data [1]
	level.Value = data [2] 
else
warn(err)
end
local success, err = pcall(function()
	datastore:SetAsync(key, items)
end
if(success) then
print("Data saved successfully")
else
warn(err)
end

I’m sorry but i don’t know where to put that in my script?

You put that near your instances

I don’t think I can post a whole script, but basically, you wrap your GetAsync and SetAsync in the pcall function. And set the data if the sucess is true.
And you should also wrap your set async just incase it fails, you’ll see the error message.

Hold on Imma take your script and wrap it for you with an Async

Why do you have two PlayerAdded connections…? You only need 1.

Thanks ------------------------------------

Do you mind sending me the full script so I’ll be able too do it faster?

-------------------------------------------------------------------------------------------------------------------

---- Variables ----

local repS = game:GetService("ReplicatedStorage")
local clickEvent = repS.remotes.ClickEvent
local clickEventUpgrade = repS.remotes.ClickEventUpgrade
local tooBroke = repS.remotes.TooBroke
local moneyCostUpdate = repS.remotes.MoneyCostUpdate

---- Cash Add ----



---------------------------------------------------------------------------

---- DataStore ----

local datastore = game:GetService("DataStoreService"):GetDataStore("GameStore") --Call "GameStore" whatever you like


game.Players.PlayerAdded:connect(function(p)
	
	---- Money ----
	
	local stats = Instance.new("Folder", p)
	stats.Name = "leaderstats"
	local money = Instance.new("IntValue", stats)
	money.Name = "Money"
	money.Value = 0
	
	---- Level ----
	
	local level = Instance.new("IntValue", stats)
	level.Name = "Level"
	level.Value = 1
	
	function moneyPay()
	
		money.Value = money.Value + 1 * level.Value
		
	end
	
	clickEventUpgrade.OnServerEvent:Connect(function()
		
		if money.Value >= level.Value * 10 then
			
			money.Value = money.Value - level.Value * 10
			
			level.Value = level.Value + 1
			
			moneyCostUpdate:FireClient(p)
			
		else
			
			tooBroke:FireClient(p)
		
		end
		
	end)
	
	local key = "user-" .. p.userId
	
	local storeditems = datastore:GetAsync(key)
	if storeditems then
		money.Value = storeditems[1] --Value of the Points, change "points" if you changed line 10
		level.Value = storeditems[2] --Value of the Wins, change "wins" if you changed line 14
	else
		local items = {money.Value, level.Value} --Change Points or Wins if you changed line 10 or 14
		datastore:SetAsync(key, items)
	end
end)

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

---------------------------------------------------------------------------

Why do you have a Server connection on a PlayerAdded connection? Bad practice.