Accessing offline player's data using DataStore2

  1. What do you want to achieve?
    Accessing data of offline players using DataStore2.

  2. What is the issue?
    DataStore2 requires the player instance which you can’t get if the player is not online.

  3. What solutions have you tried so far?
    I haven’t tried anything since I have no idea of how i’d go about it. Also haven’t found any solutions by searching.

8 Likes

DataStore2 in the end relies on Roblox DataStores so basically anything DataStore2 can do, Datastores can and the other way around. I would read the DataStore docs if I were you to get a better understanding.

You can access the data of an offline player while using the DataStore2 module, but the module itself offers no support at all. If you do want to access it anyways, you have to check how DataStore2 saves the data and then write a custom function.

Below is a function that could possibly work, but it is untested. (Based off of the reply here: How to use DataStore2 - Data Store caching and data loss prevention - #187 by Kampfkarren)

local DataStoreService = game:GetService("DataStoreService")

function getData(userId, name)
	local orderedDataStore = DataStoreService:GetOrderedDataStore(name .. "/" .. userId)
	local dataStore = DataStoreService:GetDataStore(name .. "/" .. userId)

	local pages = orderedDataStore:GetSortedAsync(false, 1)
	local data = pages:GetCurrentPage()
	if data[1] ~= nil then -- Check if you even received an entry
		return dataStore:GetAsync(data[1]) -- Get first entry with first key
	end
	return nil
end
6 Likes

You can access offline player’s data with normal DataStore using their key.
The thing with DataStore2 is that the ‘key’ is the player instance, and the player instance only exists when they’re ingame.

I misunderstood you in the post thought you meant offline as in no internet and just on your computer… Unless datastores save the current session? ( Which I doubt and never heard of it seen )

Ah, I meant as in not currently in-game : )

1 Like

First thing first, to get confusion out of the way, as @BasedFX said, there is littearly no difference between Datastore and Datastore in terms of how things work, using either one of them makes no difference.

A player being offline or online, or a player being on a certain server or not, doesn’t effect wether you can retrieve back the data. As long as a Datastore is created, and the information stored of the offline player is stored in that Datastore, where the key is that player’s UserId, can be retrieve if he’s not in the current server, of course if you knew his UserId. If you wanted to retrieve the data using his name, just make the key used to store his name instead of his UserId.

bro this is a stupid post omg lol bad person

(this is all wrong, datastroe2 needs a player and not a userid you can’t use userid)

2 Likes

Had this issue, ported anything I want to use offline to DS and anything I want to use while the player’s online to DS2. :roll_eyes:

I think you could probably change the way DataStore2 works if you go inside the module yourself - instead of using a player instance, you could instead use a UserId or some other string that uniquely identifies the user. The thing you need is a playerlist that is already available to every server, that holds every player that ever joined (and their datastore key). That sounds pretty intensive if you have a large game.

How are you planning to find out which players you are checking the datastores for? Are you checking all the datastores every once in a while, only certain players, or for logging purposes, etc… This would be good to know since the solution to your problem has different unique solutions.

I want to access x player’s ban data to be able to ban them even though they’re not in-game.

Do you have a list then of players that are “bannable”, a strike list, etc. that you have on-hand in each server? If you do, then it’s as simple as saving their UserId or datastore key that you use with the player list, and requesting their datastore.

You don’t even need DataStore2 for this, a simple :GetDataStore() would be enough for this since you only need one key, and not a backup system.

local DS = game:GetService("DataStoreService")
local Bans = DS:GetDataStore("Bans")

local function IsBanned(userId)
	local data = Bans:GetAsync(userId)
	return data ~= nil and data or false
end

local function SetBanned(userId, value)
	Bans:SetAsync(userId, value)
end

IsBanned(1) would return the ban status for the user Roblox, false indicating the player is not banned and true indicating they are banned.
SetBanned(1, true) would ban the user Roblox while SetBanned(1, false) would unban the user Roblox.

This entire thing really doesn’t need DataStore2, so try to keep DataStore2 for user data that you would like to have a backup at all times.

7 Likes

Is it fine to use “2 datastores”?
Should I not try and get it to work with DataStore2?

It’s fine to use 2 DataStores. You won’t pass the budget limit anyways when you use DataStore2 and that DataStore properly.

Key word properly. You do need to make sure it doesn’t throttle, maybe if you have an auto-save then make it so one round it saves the data store the other - datastore2, or, just make a variable for the player so you know when was the last time you updated or something, so it’s not too often.

Just for future reference, the correct code is:

local DataStoreService = game:GetService("DataStoreService")

function GetData(storeName, userId)
    local orderedDataStore = DataStoreService:GetOrderedDataStore(storeName .. "/" .. userId)
    local dataStore = DataStoreService:GetDataStore(storeName .. "/" .. userId)

    local pages = orderedDataStore:GetSortedAsync(false, 1)
    local data = pages:GetCurrentPage()
    if data[1] ~= nil then -- Check if you even received an entry
        return dataStore:GetAsync(data[1].key) -- Get first entry with first key
    end
    return nil
end
5 Likes

Jajajajajja, i am stupid, thanks for the solución, respect for you

Isn’t it creepy how all your data can be accessed even if you’re offline? So wild to think about.

More salvaje is what You loss the data

no estas a salvo, estan dentro de tus paredes