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")

game.Players.PlayerAdded:Connect(function(Player)

	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)
	end)
	
	if success then
		print("Got Data!")
	else
		print("Player has no data, New.")
	end
	
	if err then
		print(err)
		
		print("Coulden't get Data, Retrying...")
		Data = DataStoreMain:GetAsync(Player.UserId)
		
		Tries = Tries + 1 
	end
end)


game.Players.PlayerRemoving:Connect(function(Player)
	
	
	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)
	end)
	
	if success then
		print("Successfully saved data!")
	end
	
	if err then
		print("Coulden't save Data!")
		print(err)
	end
end)

game:BindToClose(function()
	for _, v in pairs(game.Players:GetPlayers()) do
		print("Shutting down server")
	end
	
    wait(5)
end)

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.

2 Likes

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.

2 Likes

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.

2 Likes

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.
    }
end)
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?