How should I save which player used which promo code, considering they will be deleted at some point?

Greetings,

I’m making some promo codes for my game, i’ve scripted them but now i’m not sure how I should go about saving that a player used a certain code. This is because these codes will be removed at some point and so i don’t want to fill data store with stuff that will disappear at some point. What do you suggest?

thank you for your time

Use a Table Datastore and a Folder in plr.

And put like the strings into folder
I will leak my code:

Now the datastore code to load in

--//Codes
	if Codes then
		for i,v in pairs(Codes) do
			for i,v in pairs(Codes) do
				if not CodesFolder:FindFirstChild(string.lower(v)) then
					local t = Instance.new("StringValue",CodesFolder)
					t.Name = string.lower(v)
				end
			end
		end
	end

to save it

local folder = plr.Codes
		for i,v in pairs(folder:GetChildren()) do
			table.insert(tab.Codes,v.Name)
		end```

say a player uses a code, then i guess a value is created in this folder, and when they log out the contentes of this folder are stored in a table and saved. When they come back the table is then turned again into values in this folder. but what when all the codes saved have been removed? then there will be a bunch of strings that are not relevant any more, clogging both my game and data store
this is the source of my dilemma

If you want to do that you could use
tick()
or os.time()

You probably know how to make a expire date system.

grafik

local https = game:GetService(“HttpService”)
print( https:JSONEncode( os.date("*t", os.time()) ) )
and putting that thing into a StringValue inside the Codes name
and then making a extra script that decodes/encodes it.

grafik

that just seems overly complicated for no real reason

1 Like

What @ElectricalSpy 's code is basically doing is adding all of the codes that they have used to a table, then uploading that table to a datastore. Datastore can handle something like 500,000 characters, so as long as you don’t have like 50,000 codes over the course of your game, you should be fine. Mose games have a max of 100 codes or so over the entire lifespan of the game, so this won’t be a problem at all for you.

I mean, everytime they join you could check if the values in the table exist as real codes. If they do not, don’t make the value. Next time they leave, their folder should only have values that exist so it all works out. Lemme know if this wouldn’t work.

oh ye i already though of this it would work fine. I’m just worried the data store would then have a bunch of stuff that’s gone. Is that a problem?

Man I never actually implemented this, I had a brain fart moment regarding the timing and couldn’t do it. I know how to do it now but it’s a little too late to implement it.

That actually won’t happen with this method. Here is how it cannot happen:

1. A player redeems a code named “LIMITED_CODE”.
2. The player leaves.
3. The code expires after an hour or so.
4. The next day, the player joins.
5. Since the code no longer exists, we won’t add the value.
6. After playing for a bit, they leave the game.
7. Since the folder is only full of active codes, we never end up saving any inactive ones.

The only time an old code is saved is if they never join the game again.

TLDR:
You won’t have to worry about that. This method should work perfectly.