DataStore not Working!

  1. What do you want to achieve? Keep it simple and clear!
    Im trying to make a DataStore that saves and loads your leaderstats data whenever you leave/join the game.
  2. What is the issue? Include screenshots / videos if possible!
    The code will not save the data and it does not send out any error messages.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried looking around the Developer Hub but the closest thing I could find was to check if the data I was saving wasn’t nil. So I used a print block to check if my data I was saving “save1” wasn’t nil and it was, but I don’t know what to do with that knowledge. I am a bit new to DataStores. Any help would be appreciated.
local DS = game:GetService("DataStoreService")
local SaveDataStore = DS:GetDataStore("SaveDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	local plrkey = "id_"..plr.UserId
	local save1 
	
	local success, errormessage = pcall(function()
		save1 = SaveDataStore:GetAsync(plrkey)
	end)
	
	if success then
		if save1 then
			plr.leaderstats.Taps.Value = save1
		end
		print(save1) -- This is what prints as "nil"
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local plrkey = "id_"..plr.UserId
	local save1 = plr.leaderstats.Taps.Value
	
	local success, errormessage = pcall(function()
		SaveDataStore:SetAsync(plrkey, save1)
	end)
	
	if success then
		print(save1)
	else 
		print("There was an Error")
		warn(errormessage)
	end
end)

I think the problem is your not using SetAsync(). Instead do SaveDataStore:SetAsync(plrkey, plr.leaderstats.Taps.Value)

Also I don’t think you need to use if save1 then because your already getting the player user id with if success then.

You did a huge mistake in this part:

local success, errormessage = pcall(function()
	SaveDataStore:SetAsync(plrkey, save1)
end)

Please, do not use :SetAsync() for updating data! Even the API wiki mentions that:

So you have to change saving part to this:

function updateData()
    return save1
end

local success, errormessage = pcall(function()
	SaveDataStore:UpdateAsync(plrkey, updateData)
end)

More information about :UpdateAsync():


Also, you aren’t checking if data is exists:

game.Players.PlayerAdded:Connect(function(plr)
	local plrkey = "id_"..plr.UserId
	local save1 
	
	local success, errormessage = pcall(function()
		save1 = SaveDataStore:GetAsync(plrkey)
	end)
	
	if success then
		if save1 then
			plr.leaderstats.Taps.Value = save1
		else
            local success, errormessage = pcall(function()
		        SaveDataStore:SetAsync(plrkey,default_number)
	        end)

            save1 = default_number
        end
		print(save1)
	end
end)

Thank you for pointing out I used SetAsync instead of UpdateAsync, I totally forgot about that. I did the changes to my code like you said to but get this error now. Any ideas?

This is my code

local DS = game:GetService("DataStoreService")
local SaveDataStore = DS:GetDataStore("SaveDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	local plrkey = "id_"..plr.UserId
	local save1 
	
	local success, errormessage = pcall(function()
		save1 = SaveDataStore:GetAsync(plrkey)
	end)
	
	if success then
		if save1 then
			plr.leaderstats.Taps.Value = save1
		else
			local success, errormessage = pcall(function()
				SaveDataStore:SetAsync(plrkey, 0)
			end)

			save1 = 0
		end
		print(save1) 
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local plrkey = "id_"..plr.UserId
	local save1 = plr.leaderstats.Taps.Value
	
	local success, errormessage = pcall(function()
		SaveDataStore:UpdateAsync(plrkey, save1)
	end)
	
	if success then
		print(save1)
	else 
		print("There was an Error")
		warn(errormessage)
	end
end)

:UpdateAsync takes a function as it’s 2nd argument.
Look at the example code in this page if you don’t know what I’m talking about:

1 Like

So like this?

game.Players.PlayerRemoving:Connect(function(plr)
	local plrkey = "id_"..plr.UserId
	local save1 = plr.leaderstats.Taps.Value
	
	local function updateData()
		return save1
	end
	
	local success, errormessage = pcall(function()
		SaveDataStore:UpdateAsync(plrkey, updateData)
	end)
	
	if success then
		print(save1)
	else 
		print("There was an Error")
		warn(errormessage)
	end
end)

I tried it this way right now and it doesn’t work and just doesn’t print an error.

I tried to simulate your problem with your code, and I had no issues.

If you’re using roblox studio, make sure you published your game.
After that go to Game Settings > Security:

If Enable Studio Access to API Services option is turned off, turn on the option. This allows to Roblox Studio to access API services such as DataStores.

After that, click the Save button and try to test the code again.