(DataStore) GetAsync errors nil value

I’m working on a datastore which stores the value inside player:
I haven’t write the SetAsync yet but I can’t even load it:

	local data = {}
	local DS_Folder_DataStore = dss:GetDataStore("DS_Folder_DataStore")
	
	local emotesData = {}
	local DS_Folder_EmotesData = dss:GetDataStore("DS_Folder_EmotesData")

for i,v in pairs (plr.DataStores:GetChildren()) do
	if v then
		table.insert(data, {key = plr.UserId,data_name = v.Name, value = v.Value})
	end
end

for i,v in pairs (plr.emotesFolder:GetChildren()) do
	if v then
		table.insert(emotesData, {key = plr.UserId,data_name = v.Name, value = v.Value})
		print("data_name="..v.Name.."Value="..v.Value.."key=".. emotesData[i].key)
	end
end

for i = 1,#data do
	if data[i].data_name ~= nil and data[i].value ~= nil then
		plr.DataStores[data[i].data_name].Value = DS_Folder_DataStore:GetAsync(data[i].key)
	end
end

for i = 1,#emotesData do
	if emotesData[i].data_name ~= nil and emotesData[i].value ~= nil then
		plr.emotesFolder[emotesData[i].data_name].Value = DS_Folder_EmotesData:GetAsync(emotesData[i].key)
	end
end

For the First Datastore DS_Folder_DataStore works fine, however for the emotes one, it errors:
image

I have no clue why it happens when they are basically the same thing, they just use a different table and DataStore but their scenario are same.
For the values that DS_Folder_DataStore stores, they are all IntValue
image

For the values that DS_Folder_emotesFolder store, they are all Strings:
image

Is the value type actually the root cause of the problem? I don’t see a direct relationship between the value type and the DataStore.

What code specifically is at line 45? You haven’t specified that here and I’m not up for counting.

1 Like

Oops, forgot to mention that
It’s the third column count from the bottom from the code that I provided

I don’t think it’s necessarily that the value types are different or anything. Your code assumes that a non-nil value will be retrieved on every GetAsync call and attempts to set it to a ValueObject’s value. Therefore, if there’s a nil in the DataStore, it attempts to set a value to nil.

I’d strongly recommend revising the code here to fetch first, verify values and then set.

I think it’s because IntValue will set to 0 when you attempt to set it to nil but not for StringValues? Thanks though, I’ll verify values first now.

Edit: Solved by:

if emotesData[i].data_name ~= nil and emotesData[i].value ~= nil and DS_Folder_EmotesData:GetAsync(emotesData[i].key) ~= nil then
1 Like