Redemption Codes System

Oh shoot, I found place where I need to save it

1 Like

I tried to save it after player being rewarded

Feel free to message me, I can go more into detail there.

Idk what to type in () at line 36 and 60

When SetAsync_Codes_DataStore()

I have tried too much methods, but them isn’t working

I don’t think you’re saving it right. Try using the SetAsyn_Codes_DataStore() function I preset

After the award? If not, then I Idk

Yes.


I tried, and it’s not working.

What does output say?


It says 13 (maxredeems value), but it doesn’t decreasing after every use

Okay, I need to sleep, so let’s talk tomorrow!

I think it is How would you go about making a twitter codes system with limited usages?

So, do you know how to fix it?

It looks like you’re hitting the datastores saving limit

No, it’s working. It’s because I created a script that saving it to datastore everytime it’s changing.

Try this

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.
			end
			
			SetAsync_Codes_DataStore()
			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
				SetAsync_Codes_DataStore()
			end
		end
	end
end)
1 Like

Okay, but I can’t do it rn.
.
.
.
.
.

1 Like

It doesn’t working, and also I checked the code, and can’t see any lines that decreasing MaxRedeems value everytime you use it.