Key Locking: A Versatile Method of Grabbing Random DataStore Values

Introduction

You ever find those games on the platform, that allows the user to create a message, leave it, and let other players discover it even if the user isn’t in the game? Well, it’s definitely possible, but is there a tutorial out there? There may be tutorials out there explaining, but they probably don’t explain the juiciness behind it. I will tell you a little system I discovered that will help you to it. I call it, key locking.

What is key locking?

Key locking is a method where players can save the preferred amount of information as a regular datastore, and can be discovered or fetch specifically. The reason why it’s called key locking is because it chooses a special unique key other datastore keys don’t have, and translates it between a datastore and an ordereddatastore. Also, the key thoroughly looks through the ordereddatastore to check if there’s any existing keys, and if there is, it replaces it with another unique one.

Lets say I wanted to create a game where players can create messages, put it in a bottle, wash it away, and wait a couple days for that information to be found by others. With key locking, this would definitely be possible. This game already exists, it’s here. It’s the epitome of these type of games.

I’m not trying to say that any of these games use these method, nor am I saying this is the best and most efficient method, but it’s a basic version on how to do so.

Examples

This is a basic version of what it might look like. If we examine it, it saves it on both the datastore and ordereddatastore.

local DataStore = DataStoreService:GetDataStore("MyDataHere")
local OrderedDataStore = DataStoreService:GetOrderedDataStore("MyOtherDataHere")

local info = {
    UserId = 1,
    Message = "Hello World!"
}

local randomCode = codemodule.new() -- 1Jw401M for ex.

DataStore:SetAsync(randomCode, info) -- didn't set pcalls, but please do.
OrderedDataStore:SetAsync(randomCode, 1) -- we can set the numerical data for personal use.

-- I suggest using DataStore2 if you're looking for data loss prevention

Common Asked Questions

Q: Why can’t you just save the data on the ordereddatastore in the key?
A: The limit of amount of characters is 50. It can save tables with JSONENCODE, but
it’d have to be small.

Q: Can’t we just use the player’s userid as the code?
A: Yes, but it can have some setbacks.

Q: You know, other people discovered another method similar to this before you, right?
A: Yes, but I just want to cover the basics on how they might do.

If there’s any better alternatives, please say so.