Datastore Key Storage

As most of you know, datastores dont let you get a list of all keys in a datastore.
This is why i created this module. Whenever you assign a key in a datastore, you just call this module function and it stores it for you.
Then when you need a list of all keys, like seeing the amount of items stored in the entire datastore, money, etc. you call the GetKeys function.
I know ordereddatastores are a thing but it doesnt really go far from ordering them. This is why i created this. (I was also bored so ye)

Source:

local module = {}

local dds = game:GetService("DataStoreService")

module.COUNTER_KEY = "DatastoreKeyStorage"

function module.CreateStorage(datastorename)
	local curStorage
	pcall(function()
		curStorage = dds:GetDataStore(module.COUNTER_KEY):GetAsync(datastorename)
	end)
	if curStorage then
		return
	end
	dds:GetDataStore(module.COUNTER_KEY):SetAsync(datastorename,{})
end

function module.AddKey(datastorename,key)
	local curStorage
	pcall(function()
		curStorage = dds:GetDataStore(module.COUNTER_KEY):GetAsync(datastorename)
	end)
	if curStorage then
		dds:GetDataStore(module.COUNTER_KEY):UpdateAsync(curStorage,function(p)
			table.insert(p,key)
			return p
		end)
	end
end

function module.GetKeys(datastorename)
	local curStorage = {}
	pcall(function()
		curStorage = dds:GetDataStore(module.COUNTER_KEY):GetAsync(datastorename)
	end)
	return curStorage
end

return module

2 Likes

Datastore:ListKeysAsync() returns a keypages object to iterate through all the keys in a datastore…

Improvement Suggestions

The module would benefit from a RemoveKey function as well, just to keep it uptodate.
I would also add warn() or retries in case the AddKey fails, and in the updateAsync() I would want to check that the key is not already listed to avoid duplication.

5 Likes

ZAMN THAT EXISTS? LOL İ DİDNT even know

1 Like

Are you from Azerbaijan? the uppercase dotted I is done when you press the I button in the keyboard

I’d agree with Wigglyaa

1 Like

im from turkey. pretty similar language so u prob switched it up

2 Likes