How should I remove a player's data from datastores correctly?

In the code you provided, you’re simply saying if success then print string + the nonexistant error.

pcall is local success,error (I generally do suc,err to avoid function conflicts)
You’re going to want to do:

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("NAME")
local suc,err = pcall(function()
   DS:RemoveAsync("KEY")
end)
if err then
   console.warn(err)
end

Updated Version

In this updated version, the returned value of the function is passed back as a variable. This is a much more perferred option to setting empty variables from inside the pcall (not used in either example). This will work for any function where a result is returned.

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("NAME")
local suc,res = pcall(function()
   return DS:RemoveAsync("KEY")
end)
if not suc then
   console.warn(res)
end

Why res instead of error?
The old example just uses suc,err. The first variable, suc, is always going to be a boolean of whether the pcall executes without an error or not. The second variable is always a returning result. When an error occurs, that second variable will be an error, returned by pcall. You can instead return your own value for successes. This is why if not suc then is preferred over if err then.

Read more about pcall here: Pcalls - When and how to use them

11 Likes