How efficient is my "Redeem Code" and how can I optimize it?

Update: I got the script to work, and made some changes. Im still questing whether I can further optimize it.
I havent tested it yet
Here is the code:

local codeList = require(game.ServerScriptService.CodeService.CodesList)
local ds = game:GetService("DataStoreService")
local codeDataStore = ds:GetDataStore("CodeDatastore")
local codesRedeemedData = {}

local codeFunc = {}
local self = codeFunc


function self:Redeem(player, code)
	if code then --// if a code is inputed
		if self:CheckIfRedeemed(player, code)  or  self:CheckIfRedeemed(player, code) == "error" then
			return "Code already redeemed or error"	
		else 
		
		local redeem = codeList[code]
		
		--//If code is valid and the reward type is coins.
	    if redeem and redeem["Coins"] then
			table.insert(codesRedeemedData, redeem)
			---//Insert Code into the datastore
			local CoinSuccess, Coinfail = pcall(function()
				codeDataStore:SetAsync(player.UserId, codesRedeemedData)
			end)
			--// If its succesfully, then return the reward.
		      if CoinSuccess then
			  print("Succesfuly Entered Coins Code into codesRedeemedData")
			  local rewardCoins = redeem["Coins"]
				     return rewardCoins
			   elseif Coinfail then
				      return "Error"
			end
			
			
		else
	      --//If code is valid and the reward type is Pets
	    if redeem and redeem["Pet"] then
			table.insert(codesRedeemedData, redeem)
		    ---//Insert Code into the datastore
			local PetSucess, PetFail = pcall(function()
				codeDataStore:SetAsync(player.UserId, codesRedeemedData)	
			end)
				--// If its succesfully, then return the reward.	
			   if PetSucess then
			   print("Succesfuly Entered Pet Code into codesRedeemedData")
			   local rewardPet = redeem["Pet"]
						return rewardPet
				elseif PetFail then
						return "Error"
			end				
				
				--// The code entered is not found.	
		 elseif not redeem then 
				return "Code Invalid/Expired/NotFound"
				
			   end
			end
		end
	end	
end



--//Check if code is redeemed
function self:CheckIfRedeemed(player, code)
	local codeData 
	local success, err = pcall(function()
	  codeData = codeDataStore:GetAsync(player.UserId)
	end)
	if success then
		if codeData[code] then
			return true
		      else return false	
		end
	end
	
	if err then
		return "Error"
	end
end

return codeFunc
2 Likes

About self

This is not what the ‘self’ keyword is ment for. Instead, you should use it within a class method.

local someClass = {}
someClass.__index = someClass

function someClass:someMethod()
    print(self)
end

Modularity concerns

The Redeem method is really large and contains a lot of logic. I recommend to split this up into separate functions and/or methods.

Simplification

The CheckIfRedeemed method can be simplified to:

function self:CheckIfRedeemed(player, code)
	local codeData 
	local success, err = pcall(function()
	  codeData = codeDataStore:GetAsync(player.UserId)
	end)

	if success then
		return codeData[code]
	else
		warn(err)

		return false
	end
end
3 Likes