I want to check if an offline player has data in datastore.
Don’t know how to do it.
Trying to order it and list it.
local players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService"):GetDataStore("1234123")
local DataStoreService = game:GetService("DataStoreService")
game.ReplicatedStorage.RecordEvent.OnServerEvent:Connect(function(player,p)
local yes = 0
local int = Instance.new("Folder")
int.Parent = player.Arrests
int.Name = "ArrestRecord"
--local count = 0
--print(#p.Character.Arrests:GetChildren())
local userid = players:GetUserIdFromNameAsync(p)
local userdata = DataStore:GetAsync(userid);
if userdata ~= nil then
print("yea")
else
print("no")
end
Don’t see what your issue is, you’ve already resolved your own question. If GetAsync doesn’t return anything then the player doesn’t have data in the DataStore, whether or not the player is on or not. If it’s not working then could you provide more context? Three sentences isn’t really descriptive.
in my opinion, I used pcall function because it easier and you can print errors. Also you can get async from the player but you got to use a player user id!
local DataStore = game:GetService("DataStoreService"):GetDataStore("1234123")
game.Players.PlayerRemoving:Connect(function(plr)
local t = {}
local ok, no = pcall(function()
t = DataStore:GetAsync("Data")
end)
if not ok then
return
else
table.insert(t,plr,Name)
DataStore:SetAsync("Data",t)
end
end)
local function findPlayersWithData ()
local t = {}
local ok, no = pcall(function()
t = DataStore:GetAsync("Data")
end)
if not ok then
return "No player has saved data or there was an error"
else
return t
end
end)
I know the player has data because I am testing it in studio. When I run its name it says it attempts to index nil with ArrestRecord. I have no idea how I could make this work
The only instance of ArrestRecord in this script is you renaming a Folder to that in a player who fires the RecordEvent RemoteEvent. You’re going to need to provide context because it seems like you’re asking about a different issue which is that your code is erroring rather than asking how to get offline player data. Please provide the actual console error as well as relevant code snippets.
They don’t have data then. This code is expecting GetAsync to return this structure:
{
ArrestRecord = {}
}
GetAsync returns nil so the player doesn’t have anything returned with GetAsync. “Attempt to index nil with ArrestRecord” → “nil.ArrestRecord” is invalid. You should never assume with DataStores or any getter really that you’re working with non-nils.
As someone mentioned earlier, it’d be better to pcall your DataStore code so you can handle both cases of error and cases like this where a player might not have data.
local success, result = pcall(function ()
return DataStore:GetAsync(userid)
end)
if success then
if result then
-- Player has data
else
-- Player does not have data
end
else
-- GetAsync failed, DataStore outage or otherwise
warn(result)
end
Putting this out of the way though, you’re amalgamating both your instance and your DataStore code and that’s probably confusing you as well. ArrestRecord is a folder created in the player who calls this RemoteEvent. DataStores do not support instance saving but you’re treating it like you’re writing to an instance. That bottom line would only work if GetAsync returned this table:
{
ArrestRecord = {
Officer = {}
}
}
Then the result of Officer after that code executes:
Officer = {Value = "Yes"}
You need to think again about how you’re handling player data. Start first with having your code know whether or not a player’s data existed after knowing that your GetAsync didn’t fail to call. From there, if the player you queried has data then you can display that data or deserialise it into instances in whatever way you want.