Making codes that can only used once

I am trying to make this game from DuckXander so you can use codes to get in game limiteds but sometimes when a lot of people are entering the code multiple people get it and this is because I am using datastores and some data is still old and that makes it think that the code isn’t used yet while it actually already is.

This is the code that saves it I can’t show everything cause it’s like 300 lines for other things in the game.

RemoteEvents:WaitForChild("UseKey").OnServerEvent:Connect(function(player, EnteredKey)
	local GeneratedCodes = Data:GetAsync("GeneratedCodes")
	local succes = false
	for _, keyTable in pairs(GeneratedCodes) do
		if EnteredKey == keyTable[1] then
			if keyTable[2] == false then
				succes = true
				keyTable[2] = true
				break
			elseif keyTable[2] == true then
				succes = "Redeemed"
				keyTable[2] = true
				break
			end
		end
	end
	
	if succes == true then
		Data:SetAsync("GeneratedCodes", GeneratedCodes)
		AwardPlayer(player, EnteredKey, GeneratedCodes)
		RemoteEvents:WaitForChild("UseKey"):FireClient(player, true)
	elseif succes == "Redeemed" then
		RemoteEvents:WaitForChild("UseKey"):FireClient(player, "Redeemed")
	else
		RemoteEvents:WaitForChild("UseKey"):FireClient(player, false)
	end
end)

If you could help me that would be wonderful cause I have no clue on how to fix this and I have searched everywhere.

Instead of calling :GetAsync() every time a player requests to redeem a code(it might make the system slower), you can create a player data system that then you can add the redeemed code data to the player’s data, and when you load the player’s data when they join the game, you can easily check if the player has the code in their data.

Or, you can create a table that stores the players that have redeemed a code, and when they leave the game, you can just save the player and the code to a database.

There are a lot of solutions you can use for this problem.

ye I know that and I have done that before but this is different it’s like a code that everyone can use but once someone used it no one can use it ever again that’s what I am trying to make so not like twitter codes or something that you have in simulator games and stuff like that

or a code that maybe like 30 people could max use like just something like that would also be the same as what I am making but for me it’s max 1 person

also the issue isn’t that it doesn’t work the issue is that for some reason multiple people can still use it while only 1 is allowed

Maybe you can use MessagingService. You can create a queue system with MessagingService then you can only allow a certain amount of players to pass through the queue.

Never used it before but I will try this thank you for your help!