Twitter Codes System | HowTo Resource | Scripting

Hey there! I’m Bestinhop23 and I’ll help y’all out make a Twitter Codes System. I get a ton of commission requests for the same so therefore I just decided to make a Community Resource for y’all.

I know this way is far from perfect, but I think it works best for most use cases.

If it’s bad formatting in the resource, it’s because it’s my first :), I’d appreciate any kinds of feedback from you all.

So, first let’s go over what we want.

  • We want a GUI with a textbox and a button in it where the user can put the code and click “Redeem”.

  • When the user clicks redeem it gives them some in game currency or any thing of user choice.

  • There’s many ways for you to do this, I’m going to be grabbing codes off ServerStorage in-game.

Anyway, let’s get started with the GUI, you can use the one below, or make your own both will work.
(Note: The file is at the very bottom of this page.)

Here's the details to making a custom GUI, if you're not using mine.

First off, we need a toggle button which opens the main interface.

So we create a ScreenGUI under StarterGUI and name it TwitterCodesGUI.

Then, we create an image button under the same GUI, set the image to whatever you want, and format it a bit. Here’s how mine looks.

image

In-order to position it to the left side of the screen we set the position value to UDim2.new(0, 0,0.5, 0)

Now let’s make the Main GUI.

Using some Frame Formatting and setting the position to UDim2.new(0.5,0,0.5,0). Mine looks something like this.

Note: Make sure you have TextBox and a Redeem Button.

That’s it for the GUI.

After you have the GUI, set the TextBox’s Placeholder Text to something like: “Enter Code Here”.

Now, set the Frame’s Visibility to false.

image

Uncheck the checkbox shown above.

As we do not want the user to see the GUI popup as soon as they join the game.

Now for us in-order to see the Frame when we click the Toggle Button, we will create a local script under the ToggleButton.

image

Now, here’s what we write in the script.

local ToggleButton = script.Parent -- We're defining a variable which tells us the location of the button.
local MainFrame = script.Parent.Parent.Frame -- We're defining a variable which tells us the location of the Frame.

ToggleButton.MouseButton1Down:Connect(function() -- We're hooking into the event which fires when the ToggleButton is clicked.
	MainFrame.Visible = not MainFrame.Visible -- This is the code we excecute when the event fires.
	-- The above code basically tells the Frame to become invisible if it's visible, and visible if it's invisible.
end)

That’s it and now whenver we click the Toggle Button, the Frame’s Visibility gets toggled.

Feel free to go test it out.

Now to the actual part.

We create a RemoteFunction in ReplicatedStorage, which basically allows us to check something with the server and perform actions on the server as well as the client. (If you don’t understand it, then leave it be, and just grab the model and use it as it is.)

image

You can call your RemoteFunction, “CheckTwitterCodes”.

We will first create a localscript under the Frame, back in StarterGUI. So we can first get what the player entered, and then later we can check using the remote function whether the code is legitimate and if the player is trying to cheat and redeem the same code multiple times.

Here’s the code for the localscript under the Frame.

-- We first declare our variables.
local TextBox = script.Parent.TextBox -- Defining the TextBox where the code will be entered.
local RedeemButton = script.Parent.OkayButton -- Defining the redeem button.
local RemoteFunction = game.ReplicatedStorage.CheckTwitterCodes -- Defining the remote function which will help us check if the code is legitimate.
local TextBoxText -- Defining a blank variable which will get the textbox's text. (Will come in handy later)
local IsCodeLegit -- Defining a blank variable which will tell us if the code is legitimate. (Will come in handy later)

RedeemButton.MouseButton1Down:Connect(function() -- We hook into the event which fires when the button is clicked.
	TextBoxText = TextBox.Text -- Getting the textbox's text at that very given moment.
	IsCodeLegit = RemoteFunction:InvokeServer(TextBoxText) -- Invoking the server to check if the code is legitimate. Will be a true/false value.
	
	-- We will now create an IF-THEN-ELSE statement and do things depending upon the Code Legitimacy.
	if IsCodeLegit then
		-- If the code is legit then we do the following.
		-- We tell the user that the code was legit.
		TextBox.Text = "" -- We set the TextBox's Text to blank so that the placeholder text is visible.
		TextBox.PlaceholderText = "You have succesfully redeemed your code."
		wait(1.5) -- We wait sometime to let the user see that it was a success.
		TextBox.PlaceholderText = "Enter Code Here!" -- We set back the PlaceholderText to Enter Code Here for the user to enter any other code.
		-- Remember, the server will grant the user the currency or whatever you want it.
	else 
		-- This code runs when either the code was invalid or the user had already redeemed it.
		TextBox.Text = "" -- We set the TextBox's Text to blank so that the placeholder text is visible.
		TextBox.PlaceholderText = "Either the code was invalid or you have already redeemed the code." -- We let the user know the same.
		wait(1.5) -- We wait sometime to let the user see the same.
		TextBox.PlaceholderText = "Enter Code Here!" -- We set back the PlaceholderText to Enter Code Here for the user to enter any other code.
	end
end)

Before we make the server codes, we need to make sure that we have atleast some valid codes in place. So we create a folder under ServerStorage called “TwitterCodes”.

Note: My code is only for currency values under leaderstats, it will vary for others.

If you do not have a leaderstats value, use this.

In case, you do not have a currency system installed, make sure you have otherwise there is no point of having MY twitter codes system. Here’s an excellent video telling you on how to do the same.

Roblox Scripting Tutorial: How to Script a Currency System - YouTube

In case, you do not want to make the whole system you can go to the video description and get the model itself with all the scripts.

In the folder, we create a IntValue. The Value’s name will be the code which the user has to enter, and the value’s value will have to be the amount the user gets in currency if the code is redeemed.

image

image

Now to main ServerScript. We will create a script under ServerScriptService, called “TwitterCodesServer”. Here are the contents.

My code is called “Code1” and it grants the user 50 Units of my currency under Player.leaderstats, you can have an infinite number of codes.

-- We start by defining our variables.
local RemoteFunction = game.ReplicatedStorage.CheckTwitterCodes -- Locating our RemoteFunction which gets the input from the client.
local TwitterCodesFolder = game.ServerStorage.TwitterCodes -- Locating our TwitterCodes folder containing our codes.
local Codes = TwitterCodesFolder:GetChildren() -- Getting all the codes under the folder.
local CurrencyName = "Coins" -- This is your currency name stored under leaderstats, for me it's coins so yeah.
local AlreadyGranted = false -- We declare a blank variable so that we don't grant the code multiple times. (Will come in handy later)
local DataStore = game:GetService('DataStoreService'):GetDataStore("TwitterCodesStore") -- We use DataStores to make sure that the user doesn't use the code again.
local AlreadyRedeemed = false -- We declare a blank variable to check if the user has already redeemed the code. (Will come in handy later)

RemoteFunction.OnServerInvoke = function(Player, CodeEntered)
	-- This event fires whenever this remotefunction is invoked.
	-- We also get the code entered by the user as well as the instance of the user itself.
	for index, CurrentCode in pairs(Codes) do
		-- We loop through all the Codes that we have and check if we find a match.
		-- We do :lower() because if the Code1 was entered like cOde1, we still grant them the amount.
		if CodeEntered:lower() == CurrentCode.Name:lower() and not AlreadyGranted then -- We also check if the code hasn't already been granted.
			-- We then check if the code hasn't already been redeemed.
			local sucess, errr = pcall(function() -- pcall to make sure that if we have errors we deal with it properly.
				AlreadyRedeemed = DataStore:GetAsync(Player.UserId.."-"..CurrentCode.Name)
			end)
			if not sucess then -- If it's not working then we tell the client an error.
				break
			end
			if AlreadyRedeemed == nil or AlreadyRedeemed == false and not AlreadyGranted then -- If it isn't redeemed and not AlreadyGranted
				-- We give them the amount as well as tell the DataStore the same.
				AlreadyGranted = true -- We specify that the code has been granted.
				AlreadyRedeemed = true -- We specify that the code has now been redeemed.
				local suc, err = pcall(function()
					DataStore:SetAsync(Player.UserId.."-"..CurrentCode.Name, AlreadyGranted) -- We set it so that the user can't redeem it again.
				end)
				if not suc then -- If it's not working then we tell the client an error.
					break
				end
				Player.leaderstats:FindFirstChild(CurrencyName).Value = Player.leaderstats:FindFirstChild(CurrencyName).Value + CurrentCode.Value -- We grant the currency.
				return true -- We tell the client to show the user that the code worked.
			else
				return false -- Otherwise we tell the client it didn't work.
			end
		end
	end
	if not AlreadyGranted then
		return false -- If it's not granted, we tell the client it didn't work.
	end
	AlreadyGranted = false
	AlreadyRedeemed = false
	-- We set it back to default for future use.
end

That’s it. You now have a Twitter Codes System in your game.

TwitterCodes.rbxl (37.4 KB)
Here’s the main file, including everything except the leaderstats code.

Thank you for reading!

Cheers!
Bestinhop23.

17 Likes

how can i make a code redeems stuff!