:GetAsync() returns nil

Alright thx, I moved it above the PlayerAdded event.

You can’t save instance in datastores.


Every instance parent is an instance, use the method of serialization. (make it a string)

1 Like

Hey, you came to save the day again! Thanks, I’ll see if it works

This is the script that y’all have helped me create. The problem is, it always prints ‘Data exists’ even if it’s the players first time joining & it doesn’t add any BoolValues to the DiscoveredEggs folder.

local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("Eggz18")
local HTTPService = game:GetService("HttpService")
local sStorage = game:GetService("ServerStorage")

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)

	local rawDataStore = datastore:GetAsync(tostring(player.UserId)) or {}
	local discoveredDataStore = HTTPService:JSONDecode(rawDataStore)
	
	for _, value in ipairs(discoveredDataStore) do
		print(value)
	end

	--print(discoveredDataStore)

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

	if discoveredDataStore or discoveredDataStore ~= nil then
		print("Data exists")--Always prints this
		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 = player:FindFirstChild(data[4], true)

		end
	else
		print("No data exists")
		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 tab = {}
	local suc, err = pcall(function()
		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, tostring(egg.Parent)})
		end

		datastore:SetAsync(tostring(player.UserId), HTTPService:JSONEncode(tab))

	end)

	if suc then
		print("Yes sir")
	else
		print(err)
		datastore:SetAsync(tostring(player.UserId), HTTPService:JSONEncode(tab))
	end
end)

local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("Eggz18")
local HTTPService = game:GetService("HttpService")
local sStorage = game:GetService("ServerStorage")

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)

	local rawDataStore = datastore:GetAsync(tostring(player.UserId)) or {}
	local discoveredDataStore = HTTPService:JSONDecode(rawDataStore)
	
	for _, value in ipairs(discoveredDataStore) do
		print(value)
	end

	--print(discoveredDataStore)

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

	if discoveredDataStore and discoveredDataStore ~= nil then
		print("Data exists")--Always prints this
		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 = player:FindFirstChild(data[4], true)

		end
	else
		print("No data exists")
		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 tab = {}
	local suc, err = pcall(function()
		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, tostring(egg.Parent)})
		end

		datastore:SetAsync(tostring(player.UserId), HTTPService:JSONEncode(tab))

	end)

	if suc then
		print("Yes sir")
	else
		print(err)
		datastore:SetAsync(tostring(player.UserId), HTTPService:JSONEncode(tab))
	end
end)

Replace or with and.
Just heads up: Saving, Storing and Loading Items into/on the client isn’t very clever. The client can always be an exploier changing these Values and duplicate them.

1 Like

Thank you! Sorry I forgot to say: this script is in ServerScriptService. This is interesting, it’s printed the error:

DiscoveredEggs is not a valid member of Player “Players.cassiestudios”

And this line errors:

local discoveredDataStore = HTTPService:JSONDecode(rawDataStore)
Argument 1 missing or nil

Oh yes! I saw the error :slightly_smiling_face:!
However the BoolValues that you insert into the player, an exploier can change them. Change the name etc. You should store them in ServerStrorage! Anyways I‘ll just rewrite the script, so please hold on a sec…

1 Like

Yeah, good point. I’ll add it as an update but for now I’m not going to worry too much because I need to get this Easter game on as soon as possible.


local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("Eggz18")
local HTTPService = game:GetService("HttpService")
local sStorage = game:GetService("ServerStorage")

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)

	local rawDataStore = datastore:GetAsync(tostring(player.UserId)) or {}
	


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

	if rawDataStore and rawDataStore ~= nil then

local discoveredDataStore = HTTPService:JSONDecode(rawDataStore)

		print("Data exists")--Always prints this
		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 = player:FindFirstChild(data[4], true)

		end
	else
		print("No data exists")
		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 tab = {}
	local suc, err = pcall(function()
		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, tostring(egg.Parent)})
		end

		datastore:SetAsync(tostring(player.UserId), HTTPService:JSONEncode(tab))

	end)

	if suc then
		print("Yes sir")
	else
		print(err)
		datastore:SetAsync(tostring(player.UserId), HTTPService:JSONEncode(tab))
	end
end)

The first error is weird, is there an folder in the player?

1 Like

It wouldn’t take much time + it is much safer! Definitely would think about that doing it now.

1 Like

Hey, you can’t use Datastore in client.
She’s using server script.

1 Like

Yeah, you’re right… my game isn’t going to be any good if I rush it. Worse case scenario, I’ll have to release it next Easter

I know, but she stores the data on the client (the bool values, etc.)

1 Like

If you like to release it now with this unsafe variant you can swipe data when you fixed it.

1 Like

image
The discovered eggs is in players not in server storage…

1 Like

I’ve changed this:

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

to this:

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

which works. For some reason, their are no BoolValues in the DiscoveredEggs folder when I join. The first time I join it prints Data exists which is strange because it shouldn’t exist yet.

1 Like

Thanks for noticing but I have a folder in ServerStorage named DiscoveredEggs which has all the BoolValues I want to clone into the players folder.

Sorry I’m confused, what are you trying to show me?

That the instance is on the client (it’s just for the Person that called synitx)

1 Like