I want to have a code gui that you can press redeem to redeem the code.
HOW DOES IT WORK?
There would be a separate script that handles all the code rewards (in this script you can add and remove codes, make codes automatically expire, and make custom rewards such as give a player a sword give some cash or multiple different currencies)
NOTE: Can only be redeemed once (but can be re-redeemed if allowed)
This is easy to do, note that you should not store the codes on the client.
Look it up on YouTube or script your own. You can use a free model if your new to Studio.
you just need to save the codes the user has inputted successfully and check them, you can add a metatable and add _index so when the user inputs a valid code that hasn’t been inputted yet you get the message
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.