Get dictionary or value by value in Datastore

when I refer to a dictionary, I mean to store all the values in a dictionary, and when I refer to value by value it is to get several times the slot

1 Like

can you elaborate with an example code snippet?

dictionaries are so powerful, and incredibly fun to mess with.


local TUU = {
value = 666;
value2 = "JOCKER";
Value3 = "JA,JA,JA"
--------------------------------------------------ahora el otro ejemplo

it should be noted that I use datastore2

Using your dictionary TUU, we need to make it “keyed”.

To key a dictionary, you just need to take the information from it, and put everything together inside a single string.

The keyed dictionary holds all the information in a format, the format is what you decide it to be!

local MEGASTRING = "I like typing anything, but this index has no value if it doesn't do anything! Wow. Why do we need this huge string? Why can't I stop typing? Arggghhhh heeeeeeeelllllp! (insert text here) <(o.o)6 thumbs up emoji attempt LET'S GO!"

We gotta take a string like the one above, and find everything we need. When we find what we want, we need to program what that string does, or what it will become. My example has no format at first, but maybe someone can find a pattern.


string.find() treats the string like a table, if we search for “!”, it will return a potential table index, for the letter or word we search. (or nil) This is a tuple value.

string.find() in this example = 78, 78. “!” appears for the first time at index 78 inside the MEGASTRING, and ends at 78 because it is one character.

searching for “heeeeeeeelllllp!” returns 153, 168!

:rofl: :skull_and_crossbones:

You have 3 values, but we need to make them stand out. (Pattern)

There’s many data sciences you can apply, you can get REALLY creative with this next part. I’m showing a method I know to make the keyedDictionary readable by your code.

local SEPARATOR = "@" -- this can be anything you want, you're branding/organizing it!

The SEPARATOR is used to find each item we want on the string. When we find something we want, it’s still going to be one string. We need to use ANOTHER separator to read our values.
If we use _ that could work pretty easily for right now. We’ll call this one the SUBseparator!

local SUBSEPARATOR = "_"

@ is our SEPARATOR, and _ is our SUBseparator.

Separator finds the keyed value, subseparator makes it readable.
(subseparator makes your value valuable)

Let’s take your TUU dictionary, and convert it into a keyed dictionary first.

local keyedDictionary = ""

for index, value in pairs(TUU) do
	keyedDictionary = keyedDictionary..index..SUBseparator..tostring(value)..SEPARATOR
end -- make sure tostring does not error                       (this)-^

After this for loop runs, your keyedDictionary looks like this:


We need to find the first @ separator, and also refresh the string.
Let’s loop through the string until every value we want is subtracted, and at the same time, we’ll un-key the dictionary back to normal again.

local restoredDictionary = {}
while string.find(keyedDictionary, SEPARATOR) ~= nil do
	local keyedTuple = string.sub(keyedDictionary, 1, (string.find(keyedDictionary, SEPARATOR)))
	local keyedIndex = string.sub(keyedTuple, 1, (string.find(keyedTuple, SUBseparator)))
	keyedIndex = string.sub(keyedIndex, 1, (string.len(keyedIndex)-1))
	local keyedValue = string.sub(keyedTuple, (string.len(keyedIndex)+2), (string.len(keyedTuple)-1))
	restoredDictionary[keyedIndex] = keyedValue -- makes new dictionary value
	keyedDictionary = string.sub(keyedDictionary, (string.len(keyedTuple)+1), string.len(keyedDictionary))

In the code above, we run the loop if a SEPARATOR is found in the keyedDictionary, line 2

line 3, we used string.sub()

string.sub() shortens a string based on where you want to start, and where you want to end. string.sub("Henlo",1,3) returns Hen, because we chose to start at letter 1, and end at letter 3.

line 3 searched and shortened keyedDictionary to become our first index and value string. We searched for @, then shortened the string to remove everything else. keyedTuple was equal to value_666@ on the first loop.

line 4 searched and shortened keyedTuple to become value_ on the first loop.

line 5 searched and removed “_” from keyedIndex. keyedIndex was equal to value .

line 6, we used string.len()

string.len() returns a number, the length in characters a string is.
(remember to use proper quotations if you combine it with maths!)

line 6 shortened keyedTuple. It used the length of keyedIndex to set the beginning character, and finished up by setting the length to exclude @, keyedValue was equal to 666 on the first loop.

line 7, we made a new dictionary index using keyedIndex as the index, then set the index’s value to keyedValue.

line 8, we legitimately updated keyedDictionary to exclude the last keyedTuple. keyedDictionary was equal to two indexes and values,
or, value2_JOCKER@Value3_JA,JA,JA@

Letting the while do loop finish completes this experiment, you’ve successfully created a dictionary, made it into a string that can save on a DataStore, and also seen it be converted back into a table once again.

Take your string, and try to save that thing! :grinning:

hey thanks for all that, I know it’s a key and all that, remember I said I use datastore 2 so I have different methods of saving, I just wanted to know if it was better the dictionary than getting a key, and I realize it is.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.