:GetAsync() returns nil

Yeah, all the players will have different DiscoveredEggs BoolValues. For example, if I join the game & click on/collect eggs, the eggs I collected in the DiscoveredEggs folder will be equal to true.

I dont think its a good idea, how about when player collects an egg it will go into discovered egg folder instead of setting value to true.

Heres a tutorial on egg hunt game by @Alvin_Blox

1 Like

Hmm… ok that might work. I will watch this, thanks so much!

From reading all of the replies, it looks like there is a-lot wrong here. I would suggest rewriting your code and visiting a community tutorial I made.

1 Like

Could you show us the content of sStorage.DiscoveredEggs please.

This is helpful, thank you. I might rewrite my code.

just to be clear, with this code you are copying all the content of sStorage.DiscoveredEggs to player.DiscoveredEggs or am I wrong?

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

You are correct. (3 0 charrrs)

1 Like

Glad to see you’re getting so much help on this topic. No worries, you are in good hands. If all fails, my only suggestion to you is to start by storing a simple single value at first, to make sure your datastore logic works before turning it into an array or a dictionary with multiple values. Be sure to check the values that you are getting back from your datastore and make sure they look correct.

1 Like

Ok, try this code, I hope it works.

local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("Eggz14")

local players = game:GetService("Players")
local sStorage = game:GetService("ServerStorage")

players.PlayerAdded:Connect(function(player)

	local plrFolder = Instance.new("Folder")
	plrFolder.Name = "DiscoveredEggs"
	plrFolder.Parent = player

	local success, discoveredDataStore = pcall(function()
		datastore:GetAsync(player.UserId)
	end)
	print(success)
	if success then
		if discoveredDataStore then
			print("Data exists")
			for _, data in pairs(discoveredDataStore) do
				local instance = Instance.new("BoolValue")
				instance.Name = data[1]
				instance.Value = data[2]
				instance.Parent = player.DiscoveredEggs
				for _, v in pairs(data.children) do
					local child = Instance.new("BoolValue")
					child.Name = v[1]
					child.Value = v[2]
					child.Parent = instance
				end
			end
		else
			print("No data exists")--Always prints this
			for _, egg in pairs(sStorage.DiscoveredEggs:GetChildren()) do
				local instance = Instance.new("BoolValue")
				instance.Value = false
				instance.Name = egg.Name
				instance.Parent = player.DiscoveredEggs
				for _, v in pairs(egg:GetChildren()) do
					local child = Instance.new("BoolValue")
					child.Value = false
					child.Name = v.Name
					child.Parent = player.DiscoveredEggs:FindFirstChild(instance.Name)
				end
			end
		end
	end
end)

players.PlayerRemoving:Connect(function(player)
	print("Player removing")
	local tab = {}
	for _, egg in pairs(player.DiscoveredEggs:GetChildren()) do
		local childrenTab = {}
		for _, child in pairs(egg:GetChildren()) do
			table.insert(childrenTab, {child.Name, child.Value})
		end
		table.insert(tab,{egg.Name, egg.Value, children = childrenTab})
	end

	local suc, err = pcall(function()
		datastore:SetAsync(player.UserId, tab)
	end)
	if not suc then
		--print("error")
	end
	print(datastore)
end)
1 Like

Thank you so much for this, unfortunately it is still printing ‘No data exists’ & the BoolValues values aren’t saving when I rejoin. I appreciate the kind effort though

I’ll try to find the error, & if I do, I’ll send the fixed version to you & you can edit your post then I’ll mark yours as the solution. I believe you have got me the closest to fixing this.

local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("Eggz103")

local players = game:GetService("Players")
local sStorage = game:GetService("ServerStorage")

players.PlayerAdded:Connect(function(player)

	local plrFolder = Instance.new("Folder")
	plrFolder.Name = "DiscoveredEggs"
	plrFolder.Parent = player

	local success, errorMessage = pcall(function()
		datastore:GetAsync(tostring(player.UserId))
	end)
	
	if success then
		if datastore:GetAsync(tostring(player.UserId)) then
			print("Data exists")
			for _, data in pairs(datastore:GetAsync(tostring(player.UserId))) do
				print(data[1])
				local instance = Instance.new("BoolValue")
				instance.Name = data[1]
				instance.Value = data[2]
				instance.Parent = player.DiscoveredEggs
				for _, v in pairs(data[3]) do
					local child = Instance.new("BoolValue")
					child.Name = v[1]
					child.Value = v[2]
					child.Parent = player.DiscoveredEggs:FindFirstChild(instance.Name)
				end
			end
		else
			print("No data exists")
			for _, egg in pairs(sStorage.DiscoveredEggs:GetChildren()) do
				print(egg)
				local instance = Instance.new("BoolValue")
				instance.Value = false
				instance.Name = egg.Name
				instance.Parent = player.DiscoveredEggs
				for _, v in pairs(egg:GetChildren()) do
					print(v)
					local child = Instance.new("BoolValue")
					child.Value = false
					child.Name = v.Name
					child.Parent = player.DiscoveredEggs:FindFirstChild(instance.Name)
				end
			end
		end
	end
end)

players.PlayerRemoving:Connect(function(player)
	print("Player removing")
	local tab = {}
	for _, egg in pairs(player.DiscoveredEggs:GetChildren()) do
		local childrenTab = {}
		for _, child in pairs(egg:GetChildren()) do
			table.insert(childrenTab, {child.Name, child.Value})
		end
			table.insert(tab,{egg.Name, egg.Value, childrenTab})
	end

	local suc, err = pcall(function()
		datastore:SetAsync(tostring(player.UserId), tab)
	end)
	if not suc then
		print(err)
	end
	print(datastore)
end)
1 Like