I am new in datastore but if there any way to get a player id or the player by the value of the datastore? but it must be noted that there are possibilities that the player is not on the same server or is not online. So basically i want to get a player id by the value of the datastore but the player can be in another server or offline.
no, i want to get a player by the value of the datastore, example i have a save value with a value of 503283203 i want to get the player with the value 503283203
Hello. Sorry for the late reply. I was wondering upon your topic and there are two workaround methods of doing this, both of which I’m not too proud of. Anyways, here’s the first one:
You can store all the data in a single datastore entry and call it as required but this method is NOT recommended as it would create pressure on the datastore, thus might slow down saves and loads.
You can, however, use a Master Key Method. You will need to make a master entry to store the list of players whose data is stored, then call upon a loop. Here’s an example:
local ReqVal = 200 -- Make changes here
local dss = game:GetService("DataStoreService")
local myData = dss:GetDataStore("YourDataStoreHere") -- Make changes here
local SavedPlayersTable = {}
local success, masterData = pcall(function() return myData:GetAsync("MasterData") end)
if success then SavedPlayersTable = masterData else warn(masterData) end
script.YourDataLoadingEventHere.Event:Connect(function() -- Make changes here
local eligible = {}
for _,UserId in pairs(SavedPlayerData) do
local success, PlrData = pcall(function() return myData:GetAsync(UserId) end)
if success then
if PlrData ~= nil and PlrData == ReqVal then
table.insert(eligible,UserId)
end
end
end
print(eligible)
end)
script.YourDataSavingEventHere.Event:Connect(function(Player) -- Make changes here
local Success,Err = pcall(function()
myData:SetAsync(Player.UserId, script.ExampleValue.Value) -- Make changes here
if success then table.insert(SavedPlayerData,Player.UserId)
else
warn(Err)
end
end)
game:BindToClose(function()
local Success,Err = pcall(function() myData:SetAsync("MasterData",SavedPlayerData) end)
if Success then print("Success") else warn(Err) end
end)