Ok, the first thing you want to do is establish valid codes, you can do that by adding a series of stringValues in a folder in replicatedstorage, then you can add a script like this one in the textbox.
script.Parent.FocusLost:Connect(function()
local text = script.Parent.Text
for i , v in pairs(game.ReplicatedStorage.Folder:GetChildren()) do
if v:IsA("StringValue") then
if string.match(text , v.Value) then
print("code redeemed")
end
end
end
end)
local codes = {
"CodeHere";
"Code2Here";
"CodeAgain"
}
local textBox = --whereever it is located
for _, code in pairs(codes) do
if textBox.Text == code then
table.remove(codes,code)
--rest of code here
end
end
local DataStoreService = game:GetService("DataStoreService")
local codeStore = DataStoreService:GetDataStore("PlayerCodes")
local codes = {
[1] = "CodeHere";
[2] = "Code2Here";
[3] ="CodeAgain"
}
if not codestore:GetAsync(Player.Name) then
codestore:SetAsync(codes)
end
local textBox = --whereever it is located
for _, code in pairs(codestore:SetAsync(codes)) do
if textBox.Text == code then
table.remove(codestore:SetAsync(codes),code)
--rest of code here
end
end
if there is no past registry of codes the player inputed, the script adds the list of codes to the datastore.
What if i do a code for christmas “Gift” and then for next christmas they cannot use it because it is saved in ds (and i want it to be used once per year)
Or better idea, use a different datastore for each year
Example:
you have a Datastore called “2021” then save the codes to that.
Then in 2022 save the codes to a datastore “2022”, that way you won’t run into the problem of knowing which year it’s from.
I would do the latter example, but I guess if you really want when you release the update with the code for the new year erase the code from the datastore when they join. So everyone has a blank slate. That will work as long as you don’t do it when the player is redeeming a code.