Why is my DataStore not working?

Hello there, So Im making a DataStore but when I leave and rejoin it isn’t working, This is my script:

   local DataStoreService = game:GetService("DataStoreService")
local DataStoreMain = DataStoreService:GetDataStore("DataMain")


	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player

	local Kills = Instance.new("IntValue")
	Kills.Name = "Kills"
	Kills.Value = 0
	Kills.Parent = leaderstats

	local KDR = Instance.new("IntValue")
	KDR.Name = "KDR"
	KDR.Value = 0
	KDR.Parent = Player
	local Deaths = Instance.new("IntValue")
	Deaths.Name = "Deaths"
	Deaths.Value = 0
	Deaths.Parent = Player
	local Credits = Instance.new("IntValue")
	Credits.Name = "Credits"
	Credits.Value = 0
	Credits.Parent = leaderstats
	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Value = 0
	XP.Parent = Player
	local Rank = Instance.new("StringValue")
	Rank.Name = "Rank"
	Rank.Value = ""
	Rank.Parent = leaderstats
	local Tries = 0
	local Data
	local success, err = pcall(function()
		Data = DataStoreMain:GetAsync(Player.UserId)
	if success then
		print("Got Data!")
		print("Player has no data, New.")
	if err then
		print("Coulden't get Data, Retrying...")
		Data = DataStoreMain:GetAsync(Player.UserId)
		Tries = Tries + 1 

	local success, err = pcall(function()
		DataStoreMain:SetAsync(Player.UserId, Player.leaderstats.Kills.Value)
		DataStoreMain:SetAsync(Player.UserId, Player.KDR.Value)
		DataStoreMain:SetAsync(Player.UserId, Player.Deaths.Value)
		DataStoreMain:SetAsync(Player.UserId, Player.leaderstats.Credits.Value)
		DataStoreMain:SetAsync(Player.UserId, Player.XP.Value)
		DataStoreMain:SetAsync(Player.UserId, Player.leaderstats.Rank.Value)
	if success then
		print("Successfully saved data!")
	if err then
		print("Coulden't save Data!")

	for _, v in pairs(game.Players:GetPlayers()) do
		print("Shutting down server")

I tried adding the tries counter so if it errors it will print Coulden’t get Data.

I would try saving a table with the values inside them.

I tried that but it didn’t work :confused:

Are you saving the values through server or player? Try using update async.

Server, Use to use UpdateAsync, It only retreives the other value. I do not want the other value.

Are you changing the value through local?

No, Changing it through server.

If success is false that doesn’t mean that the player has no data. Success will be false if there is an error in the code. If Data is nil and Success is true the player has no data.


To add on to what @Tom_atoes is saying if a player is new you want to save the values as 0

First: You keep Setting the The player’s data.
Second: You Forgot to get the values from leaderstats
Try this:

local ToSave = {
  ["Kills"] = Player.leaderstats.Kills.Value,
  ["KDR"] = Player.leaderstats.KDR.Value,
  ["Deaths"] = Player.leaderstats.Deaths.Value,
  ["Credits"] = Player.leaderstats.Credits.Value,
  ["XP"] = Player.leaderstats.XP.Value,
  ["Rank"] = Player.leaderstats.Rank.Value
DataStoreMain:SetAsync(Player.UserId, ToSave)

You should only save data in an autosave and on PlayerRemoving. Do not save on join.


UpdateAsync should be used, not SetAsync.

1 Like

When you join and you are nil the data will go to new so you would want to save a new players data as 0

You don’t call :UpdateAsync on join. Set the data variable to a table of default values if no data is returned, do not save it.


How would I use UpdateAsync to store a table, Last time I tried doing @AD4M_4DAM method just with UpdateAsync as a function and it still didn’t save…

You’re doing a pcall Function right? If it returns error use :UpdateAsync if it not don’t

Yes I was using a pcall function…

This will overwrite your stores and throttle your datastore requests. Pseudo code:

DataStoreMain:UpdateAsync(Player.UserId, function() -- Key, Function
    return {
        Kills = Player.leaderstats.Kills.Value;
        -- save rest of values here.
1 Like

If I write the rest of the values, It will save right?

I didn’t know u only had to return it, What about the Pcall?