:GetAsync() returns nil

Hi, GetAsync() returns nil.

I’ve seen the scripter who had the same problem as me: GetAsync() returns nil - Help and Feedback / Scripting Support - DevForum | Roblox

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!

1 Like

You arent setting it to anything when the player joins, thats why.

2 Likes

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?

not the issue, but this:

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)

3 Likes

Ok, I’ll change that, thank you!

Did it have any effect on the issue?

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)

2 Likes

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.

1 Like

Thank you! I get the error ‘argument 1 missing or nil’ here though:

local discoveredDataStore = HTTPService:JSONDecode(rawDataStore)

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.

1 Like

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)

woops, I had a typo

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)
1 Like

Also, in the code I provided, can you please print out tab before the setAsync (print(tab))

1 Like

Thanks for your help! The problem is, this time it prints ‘Data Exists’ when the player first joins & has no data & this line:

print(discoveredDataStore)

prints this: table: 0x000000003c639f3f

And this line:

print(tab)

prints this: table: 0x0000000063364cff

Alright, can you please replace the print() thing with the tables with this:

for _, value in ipairs(discoveredDataStore) do
    print(value)
end

then for the other one

for _, value in ipairs(tab) do
    print(value)
end
1 Like
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.

1 Like

in your code, datastore:UpdateAsync(tostring(player.UserId), HTTPService:JSONEncode(tab)) should just be :SetAsync, as you aren’t checking the value

1 Like

Okay thanks, I don’t use datastores so thanks!

1 Like

Unfortunately, it doesn’t print anything. For some reason the BoolValues aren’t in the DiscoveredEggs folder when the player joins the game.

Thanks for your help, this prints Yes Sir but yet again, the DiscoveredEggs folder is empty when I join.

sStorage should not be in the PlayerAdded event

1 Like