Clarification on memory store behaviour

I’m attempting to make a custom server list menu for my game using memory store’s hash maps. using this code

local sortedMap = memory:GetHashMap("ServerList_" .. getPlaceName())
local jobId = if not isStudio then game.JobId else "StudioServer"  -- jobId is empty in studio


local function updateServerMemoryStore()
	pcall(function()
		sortedMap:SetAsync(jobId,  {
				playerCount = #players:GetPlayers(),
				maxPlayerCount = players.MaxPlayers,
		}, set.ServerListUpdatePeriod + 30) -- accounts for lag and whatever. 
		-- Server deletes its associated memory store key with :BindClose later on in the script.
		-- But this is for the case of a sudden crash.
	end)
end

local function deleteServerMemoryStore()
	sortedMap:RemoveAsync(jobId)
end

updateServerMemoryStore()

-- messy test code
task.spawn(function()	
	while true do
		task.wait(0.3)
		print(sortedMap:ListItemsAsync(200):GetCurrentPage(), " page") -- returns an empty array except for
		-- when updateServerMemoryStore has been called previous, and then will print the correct result
		-- for one or two iterations before going back to an empty array.
		print(sortedMap:GetAsync(jobId), " get async") -- This always returns so I know that the data is being sent to the memory store, but not always being returned by :ListItemsAsync
	end
end)

task.spawn(function()
	while true do
		task.wait(set.ServerListUpdatePeriod)
		updateServerMemoryStore()
	end
end)

I had a task.spawn while loop to test if the memory store was being updated corrected but I ran into some sorta strange behaviour with :ListItemsAsync. :GetAsync given the server’s jobId will always report the correct information but :ListItemsAsync will only return the server’s data only if :UpdateAsync had been called since the previous iteration (and occasionally it would return the correct data a second time) before returning to call an empty list.


this is with the ServerListUpdatePeriod being 3 seconds for testing purposes. Note that I have only tested this in studio.

Now it does make sense that roblox would only send back data from a memory store that has been changed, In the memory’s queue documentation it talks about an “invisibility timeout” but I don’t believe that’s related, there’s no documentation in ListItemsAsync that states that this occurs so I want to double check that this isn’t my code or a studio thing.

This is not critical to me, I can just manually :GetAsync to check if a server has been deleted and have a big persistent list of all skimmed from :ListItemsAsync, but I want to make sure my understanding of memory stores is correct or if the issue is on my end or in my code.

Hey!
This is likely just a Studio issue, ListItemsAsync can behave inconsistently there, especially with only one local server. Your code looks fine, and since GetAsync works, the data is being saved.
Try testing in a live server (like a private game), it usually works correctly there. Your fallback idea using GetAsync is solid too.

Good luck!


I was gonna kick myself for not also testing in a real game but it seems to have the same issue.

1 Like

I don’t really know then, i’ll try to see if i can find more tomorrow if you still need help