function module:checkDataStoreExists(name, key)
print("Checking datastore:", name, "key:", key)
local datastore = getDataStore(name)
local function getAsyncWrapper()
return datastore:GetAsync(key)
end
local success, result = pcall(getAsyncWrapper)
print("GetAsync result:", result)
return success and result ~= nil
end
When I run this function from a server script like so
local function CreateDatastoreStats(player)
DS:saveData("stats", player.name .. "PlayerStats", "true")
end
-- Datastore API Module Script
local DataStoreService = game:GetService("DataStoreService")
local module = {}
local function getDataStore(name)
return DataStoreService:GetDataStore(name)
end
function module:checkDataStoreExists(name, key)
print("Checking datastore:", name, "key:", key)
local datastore = getDataStore(name)
local function getAsyncWrapper()
return datastore:GetAsync(key)
end
local success, result = pcall(getAsyncWrapper)
print("GetAsync result:", result)
return success and result ~= nil
end
function module:saveData(name, key, value)
local datastore = getDataStore(name)
local success, err = pcall(function()
datastore:SetAsync(key, value)
end)
print("SetAsync success:", success)
if not success then
warn("Failed to save data: " .. err)
end
return success
end
function module:loadData(name, key, default)
local datastore = getDataStore(name)
local success, result = pcall(function()
return datastore:GetAsync(key)
end)
if success then
return result or default
else
warn("Failed to load data: " .. tostring(result))
return default
end
end
function module:updateData(name, key, transformFunc)
local datastore = getDataStore(name)
local success, err = pcall(function()
datastore:UpdateAsync(key, transformFunc)
end)
if not success then
warn("Failed to update data: " .. err)
end
return success
end
function module:incrementData(name, key, delta)
local datastore = getDataStore(name)
local success, result = pcall(function()
return datastore:IncrementAsync(key, delta)
end)
if success then
return result
else
warn("Failed to increment data: " .. tostring(result))
return nil
end
end
function module:removeData(name, key)
local datastore = getDataStore(name)
local success, err = pcall(function()
datastore:RemoveAsync(key)
end)
if not success then
warn("Failed to remove data: " .. err)
end
return success
end
return module
Server script that is interacting with the module:
local events = game.ReplicatedStorage.Events
local functions = game.ReplicatedStorage.Functions
local CheckIfNewEvent = functions:WaitForChild("CheckIfNew")
local NewPlayer = functions:WaitForChild("NewPlayer")
local DS = require(game.ReplicatedStorage.Modules.Datastore_Core)
local function CheckIfNew(player)
print("checking if player data exists!")
local player_name = game:GetService("Players"):GetUserIdFromNameAsync(player.Name)
local x = DS.checkDataStoreExists("stats", player_name.."_stats")
if x == "success" then
return true
else
return false
end
end
local function CreateDatastoreStats(player)
local player_name = game:GetService("Players"):GetUserIdFromNameAsync(player.Name)
DS:saveData("stats", player_name.."_stats", "true")
end
CheckIfNewEvent.OnServerInvoke = CheckIfNew
NewPlayer.OnServerInvoke = CreateDatastoreStats
The only other script is a local script that fires the CheckIfNew function when the player is added.
Why not just use ListDataStoresAsync, it returns a page of DataStore that are in the Experience.
Your way of checking for a DataStore is very inefficient, GetDataStore will just create an entirely new DataStore if it doesnt exist, and that is not a good thing.
I dont see any issue with that, if he’s passing argument for a specific DataStore name, im pretty sure he’d want it to be created otherwise he wouldnt?
When I try using this it just gives me the error ListDatastoresAsync result: ListDataStoresAsync is not a valid member of DataStore "DataStoreService.1046703912_stats"
function module:checkDataStoreExists(name, key)
print("Checking datastore:", name, "key:", key)
local datastore = getDataStore(name)
local Data = nil
local success, errormessage = pcall(function()
Data = datastore:GetAsync(key)
end
if not success then
warn(errormessage)
end
return success,Data
end