MemoryStoreQueue Repeat Check Only Runs Once

I’m trying to make my own matchmaking system with MemoryStoreQueue but I’ve run into a problem, I’ll show you my script first:

Script
local MemoryStoreService = game:GetService("MemoryStoreService")
local queue = MemoryStoreService:GetQueue("GameQueue")

local addToQueueEvent = game:GetService("ReplicatedStorage"):WaitForChild("AddToQueue")

addToQueueEvent.OnServerEvent:Connect(function(player)
	local addSuccess, addError = pcall(function()
		queue:AddAsync("User_"..player.UserId, 0, 30)
	end)
	if not addSuccess then
		warn(addError)
	end
end)

while true do
	print("Run")
	local readSuccess, items, id = pcall(function()
		return queue:ReadAsync(1, false, 30)
	end)
	if not readSuccess then
		wait(1)
	elseif #items > 0 then
		print(items, id)
		local removeSuccess, removeError = pcall(function()
			queue:RemoveAsync(id)
		end)
		if not removeSuccess then
			warn(removeError)
		end
	end
end

Basically, when a player clicks a button, they are added to a queue and then it’s read and right now it should print out User_ plus the players userid but that doesn’t happen. What does happen is that Run is printed only once and then never again.

Old suggestion for problem (Don't read)

After moving print("Run") about I’ve managed to find that the bit holding it up is this:

return queue:ReadAsync(1, false, 30)

If this isn’t the problem then it could be that it isn’t being correctly added to the queue here:

queue:AddAsync("User_"..player.UserId, 0, 30)

Does anyone know how to help me with this? Maybe I’ve done something wrong or the article on Memory Store is wrong because when I read through this article and tried it out in Studio, it had a bunch of errors in the script editor.

Ignore the last suggestion, I have found the actual reason. I was waiting 30 seconds here:

return queue:ReadAsync(1, false, 30)

and when I changed it to 0 it gave me the error I was looking for,
ServerScriptService.QueueManager:22: attempt to get length of a nil value
This errors at the start of this chunk of code:

elseif #items > 0 then
	print(items, id)
	local removeSuccess, removeError = pcall(function()
		queue:RemoveAsync(id)
	end)
	if not removeSuccess then
		warn(removeError)
	end
end

I’m guessing items is an error message but they’ve written it wrong in the article, does anyone know how to fix this? (Sorry I know this post is getting a bit long now but I appreciate any help!)

I am actually raging right now. 2 HOURS LATER carefully studying every single word in the docs, I finally found what went wrong.

queue:AddAsync("User_"..player.UserId, 0, 30)

Here.

It turns out that the middle is expiration and the end is priority. So it is the docs mistake:

If an admin sees this, please update the docs to be correct so no one else gets stuck on this for ages. Thanks.

3 Likes