This tutorial will be teaching you how to create a twitter code system that can be updated and used instantly without having to update the game version by migrating to latest update with just a module script to store all the twitter codes to be redeemed, the script to handle the redemption of the codes and a local script for the player to redeem to code to get in game currency.
Creating the module script to store codes
First create a module script and name it MainModule so that it can be required
You would see the default
local module = {}
return module
So within the table put the data using the format below
local module = {
Test = {
Expired = false,
Currency = 100
},
Code1 = {
Expired = false,
Currency = 100
}
}
return module
After that, publish the module by saving it to Roblox and copy lock it.
Thatâs all for the module script. You can increase the number of codes so that your players can redeem more codes and also remove codes and make codes expire at the same time.
If you update the module, just update the old module you used by overwriting the old module you used.
Ensure that you have the following remote events under Replicated Storage
Server Script to handle redemption of codes
Now this part of the script would depend on the datastore you use. For this tutorial, I would be using the normal Roblox Datastore.
First, get all the services we are going to use.
local RS = game:GetService("ReplicatedStorage") --Remote Events
local DS = game:GetService("DataStoreService") -- Save Data
local IS = game:GetService("InsertService") --Load Module
For this tutorial, I would be my own module: Asset id 6085696646
We will set up some main variables
local ModuleId = 6085696646
local CodeDS = DS:GetDataStore("Codes")
local CashDS = DS:GetDataStore("Cash")
Now letâs set up some local functions
local function Fire(plr, ty) --[[To tell players about how
the redemption of code went.
plr is player instance
ty is to tell the local script what to show.
]]
RS.RemoteEvents.Redeem.Status:FireClient(plr, ty)
end
This will tell the player if the redemption of the code was a success
local function Redeem(plr, Code, CodeTable)
--[[
This is to do the redemption of the code
plr is the player instance
Code is the code that the player entered
CodeTable is your module script dictionary that we have created
]]
local done = false
local CData
--Lets say we use leaderstats to save the data
local Cash = plr.leaderstats.Cash.Value
local success, Error = pcall(function()
CData = CashDS:GetAsync(plr.UserId)
end
if success then
for i,v in pairs(CodeTable) do
if Code == i then
Cash = Cash + v["Currency"]
done = true
end
end
else
warn(Error)
end
return done
end
This will find the amount that the player would receive if they redeemed a certain code and add it
function CheckRedeemed(PlrData, Code)
--[[
This will be to check if the player has redeemed the code before
PlrData is a dictionary of all the codes the player redeemed
Code is the code that the player wants to redeem
]]
local Not = true
for i,v in pairs(PlrData) do
if Code == v then
Not = false
end
end
return Not
end
This will check if the player has redeemed the code before
Now lets get onto the main part of the code
First get the latest addition of the module. We would be loading and deleting the module everytime a player redeems a code so that the codes that can be redeemed will always be up to date and there will be no need for the newest game version to only use new updated twitter codes.
RS.RemoteEvents.Redeem.RedeemCheck.OnServerEvent:Connect(function(plr, code)
local ModuleV = IS:GetLatestAssetVersionAsync(moduleId)
local LoadModule = IS:LoadAssetVersion(ModuleV)
local TableC = require(LoadModule:WaitForChild("MainModule"))
print(TableC)
LoadModule:Destroy()
Now we will get all the codes the player redeemed before
local Data
local success, Error = pcall(function()
Data = CodeDS:GetAsync(plr.UserId)
end)
if success then
if Data then
else
--If no data then set up an empty dictionary
Data = {}
end
else
warn(Error)
Fire(plr, 5)
return
end
And for the rest of the checks
if CheckRedeemed(Data, code) then
--Check if code has been redeemed
if TableC[code] then
--Check if code exists
if not TableC[code]["Expired"] then
--Check if code has not expired yet
if Redeem(plr, code, TableC) then
--Check if the redemption was a success
Data[#Data + 1] = code
--Add the code the player just redeemed to the dictionary of codes redeemed
local good, bad = pcall(function()
CodeDS:UpdateAsync(plr.UserId, function(old)
return Data
end)
end)
--Save the dictionary of codes redeemed
Fire(plr, 0)
else
Fire(plr, 2)
end
else
Fire(plr, 4)
end
else
if code ~= "" then
--Check if any characters were input by the player
Fire(plr, 3)
else
Fire(plr, 6)
end
end
else
Fire(plr, 1)
end
end)
Now for how the player would redeem the code
Lets have a simple code redemption area
With the following children
And a simple gui
So for the script under the click detector, you want it to show the player the redemption gui
local RS = game:GetService("ReplicatedStorage")
script.Parent.MouseClick:Connect(function(plr)
RS.RemoteEvents.Redeem.Gui:FireClient(plr)
end)
For the local script to tell the server what code the player entered, to tell the player if the redemption was a success and to close the gui
So the gui children are
So for the local script
Get the Services needed
local RS = game:GetService("ReplicatedStorage")
Receive the remote event when fired from the click detector script
RS.RemoteEvents.Redeem.Gui.OnClientEvent:Connect(function()
script.Parent.Visible = true
end)
To tell the server what code the player wants to redeem
script.Parent.TextButton.MouseButton1Click:Connect(function()
RS.RemoteEvents.Redeem.RedeemCheck:FireServer(script.Parent.TextBox.Text)
end)
Function to reset the textbox
local function BoxColour()
script.Parent.TextBox.BackgroundColor3 = Color3.fromRGB(165, 165, 165)
script.Parent.TextBox.Text = ""
end
Closing the gui
script.Parent.Close.MouseButton1Click:Connect(function()
script.Parent.Visible = false
end)
Now to tell the player if the redemption of the code was a success!Sorry, but this part is a bit messy.
RS.RemoteEvents.Redeem.Status.OnClientEvent:Connect(function(info)
local Responses = {
[1] = "Code already redeemed",
[2] = "Error in redeeming code",
[3] = "Invalid Code",
[4] = "Code Expired",
[5] = "Data not found",
[6] = "Enter a code"
}
if info == 0 then
script.Parent.TextBox.BackgroundColor3 = Color3.fromRGB(0, 255, 127)
script.Parent.TextBox.Text = "Code redeemed!"
wait(3)
BoxColour()
else
script.Parent.TextBox.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
script.Parent.TextBox.Text = Responses[info]
wait(3)
BoxColour()
end
end)
All the full codes
Module
local module = {
Test = {
Expired = false,
Currency = 100
},
Code1 = {
Expired = false,
Currency = 100
}
}
return module
Server Script
local RS = game:GetService("ReplicatedStorage")
local DS = game:GetService("DataStoreService")
local IS = game:GetService("InsertService")
local DS2 = require(game.ServerScriptService.DataFolder.DataStore2)
DS2.Combine("MainData", "Tokens")
local moduleId = 6085696646
local CodeDS = DS:GetDataStore("Codes")
local CashDS = DS:GetDataStore("Cash")
local function Fire(plr, ty)
RS.RemoteEvents.Redeem.Status:FireClient(plr, ty)
end
local function Redeem(plr, Code, CodeTable)
--[[
This is to do the redemption of the code
plr is the player instance
Code is the code that the player entered
CodeTable is your module script dictionary that we have created
]]
local done = false
local CData
--Lets say we use leaderstats to save the data
local Cash = plr.leaderstats.Cash.Value
local success, Error = pcall(function()
CData = CashDS:GetAsync(plr.UserId)
end
if success then
for i,v in pairs(CodeTable) do
if Code == i then
Cash = Cash + v["Currency"]
end
end
else
warn(Error)
end
return done
end
function CheckRedeemed(PlrData, Code)
local Not = true
for i,v in pairs(PlrData) do
if Code == v then
Not = false
end
end
return Not
end
RS.RemoteEvents.Redeem.RedeemCheck.OnServerEvent:Connect(function(plr, code)
local ModuleV = IS:GetLatestAssetVersionAsync(moduleId)
local LoadModule = IS:LoadAssetVersion(ModuleV)
local TableC = require(LoadModule:WaitForChild("MainModule"))
print(TableC)
LoadModule:Destroy()
local Data
local success, Error = pcall(function()
Data = CodeDS:GetAsync(plr.UserId)
end)
if success then
if Data then
else
Data = {
}
end
else
warn(Error)
Fire(plr, 5)
end
if CheckRedeemed(Data, code) then
if TableC[code] then
if not TableC[code]["Expired"] then
if Redeem(plr, code, TableC) then
Data[#Data + 1] = code
local good, bad = pcall(function()
CodeDS:UpdateAsync(plr.UserId, function(old)
return Data
end)
end)
Fire(plr, 0)
else
Fire(plr, 2)
end
else
Fire(plr, 4)
end
else
if code ~= "" then
Fire(plr, 3)
else
Fire(plr, 6)
end
end
else
Fire(plr, 1)
end
end)
Click Detector
local RS = game:GetService("ReplicatedStorage")
script.Parent.MouseClick:Connect(function(plr)
RS.RemoteEvents.Redeem.Gui:FireClient(plr)
end)
Local Script
local RS = game:GetService("ReplicatedStorage")
RS.RemoteEvents.Redeem.Gui.OnClientEvent:Connect(function()
script.Parent.Visible = true
end)
script.Parent.TextButton.MouseButton1Click:Connect(function()
RS.RemoteEvents.Redeem.RedeemCheck:FireServer(script.Parent.TextBox.Text)
end)
local function BoxColour()
script.Parent.TextBox.BackgroundColor3 = Color3.fromRGB(165, 165, 165)
script.Parent.TextBox.Text = ""
end
RS.RemoteEvents.Redeem.Status.OnClientEvent:Connect(function(info)
local Responses = {
[1] = "Code already redeemed",
[2] = "Error in redeeming code",
[3] = "Invalid Code",
[4] = "Code Expired",
[5] = "Data not found",
[6] = "Enter a code"
}
if info == 0 then
script.Parent.TextBox.BackgroundColor3 = Color3.fromRGB(0, 255, 127)
script.Parent.TextBox.Text = "Code redeemed!"
wait(3)
BoxColour()
else
script.Parent.TextBox.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
script.Parent.TextBox.Text = Responses[info]
wait(3)
BoxColour()
end
end)
end)
script.Parent.Close.MouseButton1Click:Connect(function()
script.Parent.Visible = false
end)
So there you go, create a system to redeem codes only by updating the module storing the code and not needing to update the entire game.
Note: Since only the updating of module is required to update the twitter codes, you can store the module on another place
This is my first tutorial, any feedback and criticism to improve would be greatly appreciated!