Error with checking if a datastore key exists

I have this function:

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

It throws the error

GetAsync result: Argument 1 missing or nil

How can I fix this?
Thanks.

local function CreateDatastoreStats(player)
	DS:saveData("stats", player.name .. "PlayerStats", "true")
end

You used player.name instead of player.Name here

1 Like

Pleeeeeasseee don’t use the players name as the DataStore key. Always use their Player ID

3 Likes

This doesn’t fix the error. It still says im missing an argument

1 Like

I just thought of that. That could have gone terribley wrong

2 Likes

May i suggest using DataStore2 module.
It is very easy to use, and has data loss prevention

1 Like
1 Like

Im looking for a way to do this avoiding other datastore modules

1 Like

Can you send all of your current scripts. I’d be happy to help fix it

1 Like

Yes.

Datastore module:

-- 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.

Please do not let client have any say in datastorage, unless for clearing their own data
Use the PlayerAdded function. Like below

game:GetService("Players").PlayerAdded:connect(function(player)
print(player.Name.. "Joined")
end)

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?

Please try load the datastore through the playeradded function, And let me know you still get the error message.

Also use player.UserId rather than using game:GetService("Players"):GetUserIdFromNameAsync(player.Name)

I tried using the playeradded function and still get the same error. Im going to try using the ListDataStoresAsync function.

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

Try this, only slightly changed. But this should work

Because ListDataStoresAsync its within DataStoreService, not the DataStore itself