Redemption Codes System

mk, I would start with DataStoreService to store the codes somewhere, along with its data, such as say, the reward money, and if you want, the amount of times it can be used.


First, lets start by setting up our DataStore, and accessing it with a name - Cloud.

local DataStoreService	= game:GetService('DataStoreService')
local CloudDataStore	= DataStoreService:GetDataStore('Cloud') --//Cloud being our DataStore name

So, when you save data, you can grab it at any time. Now since we don’t have any data saved, we’ll use “Codes_DataStore” as an example. Since DataStoreService can sometimes fail we have to wrap it in a Pcall to ensure no errors break our code.

local DataStoreService	= game:GetService('DataStoreService')
local CloudDataStore	= DataStoreService:GetDataStore('Cloud') --//Cloud being our DataStore name

local DataStore_Scope	= 'Codes'
local Seconds_In_a_Day	= 86400
local Days_Code_Active	= 7

local Codes_DataStore	= {} --//Imagine this is a already saved DataStore with a Scope of "Codes"

function SetAsync_Codes_DataStore ()
	
end

function GetAsync_Codes_DataStore ()
	while true do
		local Success, Response = pcall(function()
			return CloudDataStore:GetAsync(DataStore_Scope)
		end)
		
		if Success then
			if Response then
				Codes_DataStore = Response --//Personally, I save it to a variable for quick access.
			else
				SetAsync_Codes_DataStore()
			end
			
			break --//Break from the loop
		else
			wait(7) --//Optimal DataStore cooldown time
		end
	end
end

Now we can add our codes! Although, you don’t want to override any existing code data, so what do we do now? Since we saved our Codes DataStore locally to a variable called “Codes_DataStore,” we can check if the code already exists, or needs to be added! Assuming the code is fairly new we can add it to our Codes_DataStore dictionary! and of course we’ll need to save it to DataStores right after.

local DataStoreService	= game:GetService('DataStoreService')
local CloudDataStore	= DataStoreService:GetDataStore('Cloud') --//Cloud being our DataStore name

-----------------------------

local DataStore_Scope	= 'Codes'
local Seconds_In_a_Day	= 86400
local Days_Code_Active	= 7

local Codes_DataStore	= {} --//Imagine this is a already saved DataStore with a Scope of "Codes"

function SetAsync_Codes_DataStore () --//Basically, the same thing as GetAsync, but we're "Setting" it
	while true do
		local Success, _ = pcall(function()
			CloudDataStore:SetAsync(DataStore_Scope, Codes_DataStore) --//(Argument #2) being our Codes_DataStore Dictionary.
		end)
		
		if Success then
			break
		else
			wait(7)
		end
	end
end

function GetAsync_Codes_DataStore ()
	while true do
		local Success, Response = pcall(function()
			return CloudDataStore:GetAsync(DataStore_Scope)
		end)
		
		if Success then
			if Response then
				Codes_DataStore = Response --//Personally, I save it to a variable for quick access.
			else
				SetAsync_Codes_DataStore()
			end
			
			break --//Break from the loop
		else
			wait(7) --//Optimal DataStore cooldown time
		end
	end
end


--------------------------


local CodeA_Key = 'YourCodeNameHere'

if not Codes_DataStore[CodeA_Key] then --//If the Key "CodeA_Key," does not exist in our dictionary, then proceed to add it
	Codes_DataStore[CodeA_Key] = {
		['Cash']		= 0,
		['Timer']		= tick() + (Seconds_In_a_Day * Days_Code_Active), --//Basically, saving our current epoc time(tick) plus 7 times the seconds in a day (7 days).
		['Redeemed']	= {},
		['MaxRedeems']	= 0, --//Optional for maximum redeems
	}
	
	SetAsync_Codes_DataStore() --//We want to save the codes to DataStore for the future
end

Now, recall that data across the game. For instance, if a player decides to use the code ‘YourCodeNameHere’ (aka CodeA), you can check if the code is still active. Then, proceed to see if the player has already redeemed it. Finally, add any cash from there!

local DataStoreService	= game:GetService('DataStoreService')
local CloudDataStore	= DataStoreService:GetDataStore('Cloud') --//Cloud being our DataStore name

-----------------------------

local DataStore_Scope	= 'Codes'
local Seconds_In_a_Day	= 86400
local Days_Code_Active	= 7

local Codes_DataStore	= {} --//Imagine this is an already saved DataStore with a Scope of "Codes"

function SetAsync_Codes_DataStore () --//Basically, the same thing as GetAsync, but we're "Setting" it
	while true do
		local Success, _ = pcall(function()
			CloudDataStore:SetAsync(DataStore_Scope, Codes_DataStore) --//(Argument #2) being our Codes_DataStore Dictionary.
		end)
		
		if Success then
			break
		else
			wait(7)
		end
	end
end

function GetAsync_Codes_DataStore ()
	while true do
		local Success, Response = pcall(function()
			return CloudDataStore:GetAsync(DataStore_Scope)
		end)
		
		if Success then
			if Response then
				Codes_DataStore = Response --//Personally, I save it to a variable for quick access.
			else
				SetAsync_Codes_DataStore()
			end
			
			break --//Break from the loop
		else
			wait(7) --//Optimal DataStore cooldown time
		end
	end
end


--------------------------


local CodeA_Key = 'YourCodeNameHere'

if not Codes_DataStore[CodeA_Key] then --//If the Key "CodeA_Key," does not exist in our dictionary, then proceed to add it
	Codes_DataStore[CodeA_Key] = {
		['Cash']		= 0,
		['Timer']		= tick() + (Seconds_In_a_Day * Days_Code_Active), --//Basically, saving our with the current epoc time plus, 7 days in seconds.
		['Redeemed']	= {},
		['MaxRedeems']	= 0, --//Optional for maximum redeems
	}
	
	SetAsync_Codes_DataStore() --//We want to save the codes to DataStore for the future
end


-------------------------

RemoteEvent.OnServerEvent:Connect(function(Player, CodeKey)
	if Codes_DataStore[CodeKey] then --//Does the code exist in our Codes_DataStore?
		
		if (Codes_DataStore[CodeA_Key]['Timer'] > tick()) then --//Is Code Expired?
			
			if not table.find(Codes_DataStore[CodeA_Key]['Redeemed'], Player.UserId) then --//Did player already redeem code?
				
				--//OPTIONAL
				if (#Codes_DataStore[CodeA_Key]['Redeemed'] > Codes_DataStore[CodeA_Key]['MaxRedeems']) then --//Total Redeemed (is greater than) Max Redeem
					return
				end
				--
				
				--//Give player cash!
				table.insert(Codes_DataStore[CodeA_Key]['Redeemed'], Player.UserId) --//Add player to Redeemed list
				
			end
		end
	end
end)

This was a lot more than I thought and had to restart a couple of times. Sorry it took so long, but please feel free to ask any questions. I would love to help out as much as possible!

1 Like