Hi there! This is a simple tutorial on how to make a GUI for a code that can be redeemed! Thank you for reading this tutorial!
With that being said, let’s begin!
Step 1: Create a GUI
In order for players to redeem your code, you have to add text boxes and buttons! To do so, do the following:
- Insert a ScreenGui inside of StarterGui. You can do this by opening the Explorer in the View tab and clicking the + on StarterGui.
- Insert a Frame inside of the ScreenGui.
- Inside the Frame, insert the following into it:
1 TextButton (for submitting a code)
2 TextLabels (for title and description)
1 TextBox (to enter the code)
Optional:
1 UICorner (for a neat-looking frame)
- Open the Properties tab in View, and make all listed elements’ (except the UICorner) sizes
0.1, 0, 0.1, 0
. to make it scale correctly on most devices. You can then make it as big as you want (but don’t edit the 2nd and 4th values). - Change the properties of the UI elements and the text as you wish! Here is an example of a finished product:
- In the ScreenGui, insert a TextButton and name it CodesButton. Edit the text as you wish.
Step 2. Leaderstats
Before you can redeem a code, we need a type of currency to set up! To do this, we need to:
- Insert a Script inside of ServerScriptService (MAKE SURE IT IS A REGULAR SCRIPT OR IT WONT WORK)
- Double-click the script, and it will have some code that would say
print("Hello world!")
. Delete that, and inside of it paste the following code:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins" -- Name it whatever you want
coins.Value = 0
coins.Parent = leaderstats
end)
Step 3. Scripting
Now it is time to begin scripting the main UI! To do all of this, do:
- Name the title “Title” and the description “Description”. Name the Frame “Background”.
- Insert a LocalScript, open it up, and delete
print("Hello world!")
. - Before we start coding, we need to make one last preparation. Insert a RemoteFunction in ReplicatedStorage and name it “SubmitCode”.
- Insert another Script into ServerScriptService. In that same script, insert a ModuleScript into it. Delete all the default code in the scripts. Name the ModuleScript “CodeList” and the Script “Codes”.
- In the ModuleScript, paste the following code:
return {
["test"] = 100; -- The name of the code and the number of currency it will reward the player
["test2"] = 101; -- The name of the code and the number of currency it will reward the player
}
- In the Script named “Codes”, paste the following code:
local codesList = require(script.CodeList) -- Getting the CodeList script
game.Players.PlayerAdded:Connect(function(player)
local codesFolder = Instance.new("Folder", player) -- Making a folder for the codes
codesFolder.Name = "Codes"
end)
game.ReplicatedStorage.SubmitCode.OnServerInvoke = function(player, code) -- When the button is pressed, we will check to see if the code is real or not.
if codesList[code] ~= nil then
if player.Codes:FindFirstChild(code) == nil then
local temp = Instance.new("BoolValue", player.Codes)
temp.Name = code
player.leaderstats.Coins.Value += codesList[code]
return "Valid code"
end
return "Already redeemed"
end
return "Incorrect code"
end
- We are almost done! To finish our GUI, paste the following code into the LocalScript:
local UI = script.Parent
local background = UI.Background
local submit = background.TextButton
local textBox = background.TextBox
local button = UI.CodesButton
button.MouseButton1Click:Connect(function() -- Makes the gui visible and invisibe
background.Visible = not background.Visible
end)
submit.MouseButton1Click:Connect(function() -- When the Submit button is clicked, we use the RemoteEvent to run code from the Codes script
textBox.TextEditable = false
local message = game.ReplicatedStorage.SubmitCode:InvokeServer(textBox.Text)
textBox.Text = message
wait(2)
textBox.Text = ""
textBox.TextEditable = true
end)
The end!
Congratulations! You have just made a Code GUI! If the code does not work, feel free to message me or make a reply!