Datastore is not working at all

For some reason, my datastore doesn’t work, I tried to see if it something wrong with playerleaving but it not printing anything And it still says that there no data.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local TestPlayerData = DataStoreService:GetDataStore("TestPlayer1")

local function PlayerAdded(LocalPlayer)
	
	local Leaderstats = Instance.new("Folder",LocalPlayer)
	Leaderstats.Name = "leaderstats"
	
	local Money = Instance.new("IntValue",Leaderstats)
	Money.Name = "Money"
	
	local Gems = Instance.new("IntValue",Leaderstats)
	Gems.Name = "Gems"
	
	local DataVersion = Instance.new("IntValue",LocalPlayer)
	DataVersion.Name = "DataVersion"
	
	local Data = nil
	
	local success,errormessage = pcall(function()
		Data = TestPlayerData:GetAsync(LocalPlayer.UserId)
	end)
	if success then
		if Data then
			print(LocalPlayer.Name .. "Data has been Loaded")
			Money.Value = Data.Money
			Gems.Value = Data.Gems
			DataVersion.Value = Data.DataVersion
		else
			print("nill data :)")

			return {
				Money = Money.Value;
				Gems = Gems.Value;
				DataVersion = DataVersion.Value;
			};
		end
	end
end



local function PlayerLeaveing(LocalPlayer)
	local Leaderstats = LocalPlayer.leaderstats
	
	local success,errormessage = pcall(function()
		TestPlayerData:UpdateAsync(LocalPlayer.UserId, function(OldData)
			if OldData.DataVersion == LocalPlayer.DataVersion then
				return {
					Money = Leaderstats.Money.Value;
					Gems = Leaderstats.Gems.Value;
					DataVersion = LocalPlayer.DataVersion.Value + 1;
				}
			else
				return nil
			end
		end)
	end)
	
	if success then
		print(" Save Data")
	else
		warn(errormessage)
	end
end




Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerLeaveing)

This is a script inside serverscriptservice and Yes API services is turn on.

Are you testing this in studio or in game?

DataStores don’t work properly in studio, test it in-game.

1 Like

Stlll didn’t work in live game.

Have you checked the security tab? (game settings) Usually that’s where most errors with datastores come from, if not, I don’t know how to help.

Is Studio Access to API Services on? It’s in Game Settings > Security > Enable Studio Access to API Services. It has to be on in order for Data Stores (and other services) to work.

1 Like

Isn’t updateasync for updates? and setasync is for when someone is leave?

Not sure if this matters but still…

Yes, Studio Access to API Services is on.

Is your code in a local script?

No it not in localscript is just a script inside serverscriptservice

instead of “return nil” set a default value instead
“OldData.DataVersion” is nil and won’t equal the LocalPlayers DataVersion since “OldData.DataVersion” is nil(Assuming that LocalPlayer.DataVersion) is a Int/Number Value

1 Like

Thanks for that, but it still not saving.

Current Script
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local TestPlayerData = DataStoreService:GetDataStore("TestPlayer1")

local function PlayerAdded(LocalPlayer)

	local Leaderstats = Instance.new("Folder",LocalPlayer)
	Leaderstats.Name = "leaderstats"

	local Money = Instance.new("IntValue",Leaderstats)
	Money.Name = "Money"

	local Gems = Instance.new("IntValue",Leaderstats)
	Gems.Name = "Gems"

	local DataVersion = Instance.new("IntValue",LocalPlayer)
	DataVersion.Name = "DataVersion"

	local Data = nil

	local success,errormessage = pcall(function()
		Data = TestPlayerData:GetAsync(LocalPlayer.UserId)
	end)
	if success then
		if Data then
			print(LocalPlayer.Name .. "Data has been Loaded")
			Money.Value = Data.Money
			Gems.Value = Data.Gems
			DataVersion.Value = Data.DataVersion
		else
			print("nill data :)")

			return {
				Money = Money.Value;
				Gems = Gems.Value;
				DataVersion = DataVersion.Value;
			};
		end
	end
end



local function PlayerLeaveing(LocalPlayer)
	local Leaderstats = LocalPlayer.leaderstats

	local success,errormessage = pcall(function()
		TestPlayerData:UpdateAsync(LocalPlayer.UserId, function(OldData)
			if OldData.DataVersion == LocalPlayer.DataVersion.Value then
				return {
					Money = Leaderstats.Money.Value;
					Gems = Leaderstats.Gems.Value;
					DataVersion = LocalPlayer.DataVersion.Value + 1;
				}
			else
				return { -- deafult.
					Money = 0;
					Gems = 0;
					DataVersion = 0;
				}
			end
		end)
	end)

	if success then
		print(" Save Data")
	else
		warn(errormessage)
	end
end




Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerLeaveing)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local TestPlayerData = DataStoreService:GetDataStore("TestPlayer1")

local function PlayerAdded(LocalPlayer)
	
	local Leaderstats = Instance.new("Folder",LocalPlayer)
	Leaderstats.Name = "leaderstats"
	
	local Money = Instance.new("IntValue",Leaderstats)
	Money.Name = "Money"
	
	local Gems = Instance.new("IntValue",Leaderstats)
	Gems.Name = "Gems"

	local Data = nil
	
	local success,errormessage = pcall(function()
		Data = TestPlayerData:GetAsync(LocalPlayer.UserId.."-Gems")
	end)
	if success then
		LocalPlayer.leaderstats.Gems.Value = data
    else
	    warn(errormessage)
	end
end



local function PlayerLeaveing(LocalPlayer)
	
	local success,errormessage = pcall(function()
        DataStore:SetAsync(LocalPlayer.UserId.."-Gems", LocalPlayer.leaderstats.Gems.Value)
	end)
	
	if success then
	    print(" Save Data")
	else
	    warn(errormessage)
	end
end




Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerLeaveing)

Ok so paste this code in and let me know if this works (it should). There are a few problems with your code;

  1. You use a lot of capitalization and it gets cumbersome so I’d change that, it’s still valid tho
  2. When saving data we don’t use UpdateAsync we use SetAsync, Remember: SetAsync basically means save data, and GetAsync basically means load data.
  3. As far as I know you can not return something to a pcall

I suggest rather to do it on server than client and i dont recall “UpdateAsync”, Instead of UpdateAsync use SetAsync

im doing whole datastore script on server

Why did you use LocalPlayer? That just confused me but change UpdateAsnyc