I am unsure how I would or if I need to add an array of UserIds into SetAsync (as that was his solution).
This is my script in ServerScriptService:
local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("Eggz14")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local discoveredDataStore = datastore:GetAsync(player.UserId)--"key")
print(discoveredDataStore)--prints nil
local sStorage = game:GetService("ServerStorage")
local plrFolder = Instance.new("Folder", player)
plrFolder.Name = "DiscoveredEggs"
if discoveredDataStore or discoveredDataStore ~= nil then
print("Data exists")
for i, data in pairs(discoveredDataStore) do
print("Looping")
local instance = Instance.new(data[3])
instance.Value = tostring(data[2])
instance.Name = data[1]
instance.Parent = data[4]
end
else
print("No data exists")--Always prints this
for i, egg in pairs(sStorage.DiscoveredEggs:GetChildren()) do
if egg.Parent == sStorage:FindFirstChild("DiscoveredEggs") then
local instance = Instance.new("BoolValue")
instance.Value = false
instance.Name = egg.Name
instance.Parent = player.DiscoveredEggs
end
end
for i, v in pairs(sStorage.DiscoveredEggs:GetDescendants()) do
local instance = Instance.new("BoolValue")
instance.Value = false
instance.Name = v.Name
local instanceToString = tostring(instance.Name)
local eggNoNumbers = string.gsub(instanceToString,"%d+", "")
if sStorage.DiscoveredEggs:FindFirstChild(eggNoNumbers):FindFirstChild(instance.Name) then
local parent = player.DiscoveredEggs:FindFirstChild(eggNoNumbers)
instance.Parent = parent
end
end
end
end)
players.PlayerRemoving:Connect(function(player)
print("Player removing")
local suc, err = pcall(function()
local tab = {}
for i, egg in pairs(player.DiscoveredEggs:GetDescendants()) do
print(egg.Name)--prints correctly
print(egg.Value)--prints correctly
print(egg.Parent)--prints correctly
table.insert(tab,{egg.Name, tostring(egg.Value), egg.ClassName, egg.Parent})
end
if not datastore:GetAsync(player.UserId) then
datastore:SetAsync(player.UserId, tab)
else
datastore:UpdateAsync(player.UserId,function(oldVal)
return tab
end)
end
end)
end)
Sorry if the solution is obvious, I am very new to DataStores. Usually I would work on figuring this out myself for longer than a week but I am trying to get this Easter game on as soon as possible. Any help would be appreciated!
Thanks for replying! In this script, I’m trying to save BoolValues Names, Values, ClassNames & Parents. The BoolValues are descendants of a folder named ‘DiscoveredEggs’ inside of the player - as you can see in the PlayerRemoving function. I understand what you’re saying but (I’m confused, I apoligise) what would I need to set it to?
if not datastore:GetAsync(player.UserId) then
datastore:SetAsync(player.UserId, tab)
else
datastore:UpdateAsync(player.UserId,function(oldVal)
return tab
end)
could just be simplified to
datastore:SetAsync(player.UserId, tab)
as you aren’t validating the data (and it would probably be hard to validate it)
If not, here is a slight change of code (I think the method I am using is out of date, but it hasn’t failed me so ima keep doing it)
Code
local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("Eggz14")
local HTPPService = game:GetService("HttpService")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local rawDataStore = datastore:GetAsync(tostring(player.UserId))--"key") or {}
local discoveredDataStore = HTPPService:JSONDecode(rawDataStore)
print(discoveredDataStore)--prints nil
local sStorage = game:GetService("ServerStorage")
local plrFolder = Instance.new("Folder", player)
plrFolder.Name = "DiscoveredEggs"
if discoveredDataStore or discoveredDataStore ~= nil then
print("Data exists")
for i, data in pairs(discoveredDataStore) do
print("Looping")
local instance = Instance.new(data[3])
instance.Value = tostring(data[2])
instance.Name = data[1]
instance.Parent = data[4]
end
else
print("No data exists")--Always prints this
for i, egg in pairs(sStorage.DiscoveredEggs:GetChildren()) do
if egg.Parent == sStorage:FindFirstChild("DiscoveredEggs") then
local instance = Instance.new("BoolValue")
instance.Value = false
instance.Name = egg.Name
instance.Parent = player.DiscoveredEggs
end
end
for i, v in pairs(sStorage.DiscoveredEggs:GetDescendants()) do
local instance = Instance.new("BoolValue")
instance.Value = false
instance.Name = v.Name
local instanceToString = tostring(instance.Name)
local eggNoNumbers = string.gsub(instanceToString,"%d+", "")
if sStorage.DiscoveredEggs:FindFirstChild(eggNoNumbers):FindFirstChild(instance.Name) then
local parent = player.DiscoveredEggs:FindFirstChild(eggNoNumbers)
instance.Parent = parent
end
end
end
end)
players.PlayerRemoving:Connect(function(player)
print("Player removing")
local suc, err = pcall(function()
local tab = {}
for i, egg in pairs(player.DiscoveredEggs:GetDescendants()) do
print(egg.Name)--prints correctly
print(egg.Value)--prints correctly
print(egg.Parent)--prints correctly
table.insert(tab,{egg.Name, tostring(egg.Value), egg.ClassName, egg.Parent})
end
datastore:SetAsync(tostring(player.UserId), HTTPService:JSONEncode(tab))
end)
end)
(I added Nennocyte’s suggestion to this code as well)
whenever you’re referencing player.UserId, put it in a tostring() since that property returns an int and datastore accepts strings as keys, might not solve the problem but good to try anyway.
that’s because the datastore isn’t returning anything so it can not decode, did you try turning it into a string? i don’t think turning it into a json string is needed.
Ok, I’ve changed it to this but it still prints nil
local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("Eggz14")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local discoveredDataStore = datastore:GetAsync(tostring(player.UserId))--"key")
print(discoveredDataStore)--prints nil
local sStorage = game:GetService("ServerStorage")
local plrFolder = Instance.new("Folder", player)
plrFolder.Name = "DiscoveredEggs"
if discoveredDataStore or discoveredDataStore ~= nil then
print("Data exists")
for i, data in pairs(discoveredDataStore) do
print("Looping")
local instance = Instance.new(data[3])
instance.Value = tostring(data[2])
instance.Name = data[1]
instance.Parent = data[4]
end
else
print("No data exists")--Always prints this
for i, egg in pairs(sStorage.DiscoveredEggs:GetChildren()) do
if egg.Parent == sStorage:FindFirstChild("DiscoveredEggs") then
local instance = Instance.new("BoolValue")
instance.Value = false
instance.Name = egg.Name
instance.Parent = player.DiscoveredEggs
end
end
for i, v in pairs(sStorage.DiscoveredEggs:GetDescendants()) do
local instance = Instance.new("BoolValue")
instance.Value = false
instance.Name = v.Name
local instanceToString = tostring(instance.Name)
local eggNoNumbers = string.gsub(instanceToString,"%d+", "")
if sStorage.DiscoveredEggs:FindFirstChild(eggNoNumbers):FindFirstChild(instance.Name) then
local parent = player.DiscoveredEggs:FindFirstChild(eggNoNumbers)
instance.Parent = parent
end
end
end
end)
players.PlayerRemoving:Connect(function(player)
print("Player removing")
local suc, err = pcall(function()
local tab = {}
for i, egg in pairs(player.DiscoveredEggs:GetDescendants()) do
print(egg.Name)--prints correctly
print(egg.Value)--prints correctly
print(egg.Parent)--prints correctly
table.insert(tab,{egg.Name, tostring(egg.Value), egg.ClassName, egg.Parent})
end
datastore:SetAsync(tostring(player.UserId), tab)
end)
end)
local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("Eggz14")
local HTPPService = game:GetService("HttpService")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local rawDataStore = datastore:GetAsync(tostring(player.UserId)) or {}
local discoveredDataStore = HTPPService:JSONDecode(rawDataStore)
print(discoveredDataStore)--prints nil
local sStorage = game:GetService("ServerStorage")
local plrFolder = Instance.new("Folder", player)
plrFolder.Name = "DiscoveredEggs"
if discoveredDataStore or discoveredDataStore ~= nil then
print("Data exists")
for i, data in pairs(discoveredDataStore) do
print("Looping")
local instance = Instance.new(data[3])
instance.Value = tostring(data[2])
instance.Name = data[1]
instance.Parent = data[4]
end
else
print("No data exists")--Always prints this
for i, egg in pairs(sStorage.DiscoveredEggs:GetChildren()) do
if egg.Parent == sStorage:FindFirstChild("DiscoveredEggs") then
local instance = Instance.new("BoolValue")
instance.Value = false
instance.Name = egg.Name
instance.Parent = player.DiscoveredEggs
end
end
for i, v in pairs(sStorage.DiscoveredEggs:GetDescendants()) do
local instance = Instance.new("BoolValue")
instance.Value = false
instance.Name = v.Name
local instanceToString = tostring(instance.Name)
local eggNoNumbers = string.gsub(instanceToString,"%d+", "")
if sStorage.DiscoveredEggs:FindFirstChild(eggNoNumbers):FindFirstChild(instance.Name) then
local parent = player.DiscoveredEggs:FindFirstChild(eggNoNumbers)
instance.Parent = parent
end
end
end
end)
players.PlayerRemoving:Connect(function(player)
print("Player removing")
local suc, err = pcall(function()
local tab = {}
for i, egg in ipairs(player.DiscoveredEggs:GetDescendants()) do
print(egg.Name)--prints correctly
print(egg.Value)--prints correctly
print(egg.Parent)--prints correctly
table.insert(tab,{egg.Name, tostring(egg.Value), egg.ClassName, egg.Parent})
end
datastore:SetAsync(tostring(player.UserId), HTTPService:JSONEncode(tab))
end)
end)
players.PlayerRemoving:Connect(function(player)
print("Player removing")
local suc, err = pcall(function()
local tab = {}
for i, egg in ipairs(player.DiscoveredEggs:GetDescendants()) do
print(egg.Name)--prints correctly
print(egg.Value)--prints correctly
print(egg.Parent)--prints correctly
table.insert(tab,{egg.Name, tostring(egg.Value), egg.ClassName, egg.Parent})
end
datastore:SetAsync(tostring(player.UserId), HTTPService:JSONEncode(tab))
end)
end)
It should be like this:
players.PlayerRemoving:Connect(function(player)
print("Player removing")
local suc, err = pcall(function()
local tab = {}
for i, egg in ipairs(player.DiscoveredEggs:GetDescendants()) do
print(egg.Name)--prints correctly
print(egg.Value)--prints correctly
print(egg.Parent)--prints correctly
table.insert(tab,{egg.Name, tostring(egg.Value), egg.ClassName, egg.Parent})
end
datastore:SetAsync(tostring(player.UserId), HTTPService:JSONEncode(tab))
end)
if suc then
print("Yes sir")
else
print(err)
datastore:UpdateAsync(tostring(player.UserId), HTTPService:JSONEncode(tab))
end
end)
This is just in case a error happens which can cause data lost.