How To Create A GUI Currency Giver

Introduction

Making a GUI currency giver is a pretty simple task to complete and might be helpful to help boost something of yours quickly.

I first wanted to make something like this when I saw a video created by ZephPlayz with Roblox, when Zeph came into the game he found a R$ GUI giver that “gave” him Robux (I Played ROBLOX'S Free Robux Game.. - YouTube (10:16-11:25)). I thought to myself, well what if I made one to give currency to someone in my game? I started constructing and with a little help from dev forum I was able to complete this project and share it with everyone.

Please note this is not a very necessary thing to know when programming a game, but it is pretty fun to play with and good to learn from.

Set Up

The first thing you want to do when setting up this GUI is obviously create one in StarterGui, and build a model/part to open up the currency giver. In the model/part you made, add a Click Detector and (Server)Script. In the script add a remote event to send to the client for when the click detector has been clicked.

local debounce = false
    script.Parent.ClickDetector.MouseClick:Connect(function(plr)
         if debounce == false then  -- add a debounce, so it can't be clicked so many times
		    debounce = true
	        game.ReplicatedStorage.FatbuxG:FireClient(plr) --FatbuxG is the name of my remote event
            wait(16) -- we add FireClient(plr) to make sure it sends to only the person who clicked it
            debounce = false
        end
    end)

After some time of decorating and scripting this, I ended up coming with this example:

Now we need to make it so the GUI will appear when it receives the remote event. In a local script I added this:

game.ReplicatedStorage.FatbuxG.OnClientEvent:Connect(function()
    script.Parent.Parent.Visible = true -- make sure to add all of your things in the Frame and set them to either visible or not
    script.Parent.Parent.giving.Visible = false
    script.Parent.Visible = true
    script.Parent.Parent.TextBox2.Visible = true
    script.Parent.Parent.TextBox.Visible = true
    script.Parent.Parent.text.Visible = true
    script.Parent.Parent.enter.Visible = true
end)

script.Parent.MouseButton1Click:Connect(function() -- function to close GUI if denied
	script.Parent.Parent.Visible = false
end)

Creating The GUI Function

Now that everything has been setup, we can start to create the actual function of the GUI. Let’s first start by making it so players can only type numbered strings into the amount textbox (the amount of currency someone types in). Insert a local script into your amount textbox. You may do this another way, I just personally like this more (more info on this: TextBox Masks and Formats):

function Format(t) -- t is the text from the textbox
    return t:gsub("%D", "") -- only allows numerical strings
end

script.Parent:GetPropertyChangedSignal("Text"):Connect(function() -- we use GetPropertyChangedSignal to help determine when the text changes in the textbox
	script.Parent.Text = Format(script.Parent.Text) -- makes sure that it fits the format
end)

Now that we have that fancy cool technique done, lets move on to the actual function of the GUI. As always we use debounce to help ensure that a player can’t repeatedly click the button, and possibly break the function. Since I am using a local script in the textbox though, I need to define the textbutton and other children in the frame when confirming a transaction. After that you can add in anything you want the GUI to do before it fire’s a remote event and some data back to the server. It is important to note that, ANYTHING TYPED IN A TEXTBOX IS A STRING! so we will need to use tonumber to turn the string into a number, just like using tostring to convert a number to a string. Once you are done with that, you need to make sure you send the number of the text in the textbox to the server when firing. It should look similar to this:

local event = game.ReplicatedStorage.FatbuxP  
local a = script.Parent
local c = script.Parent.Parent.TextBox2 	
event:FireServer(tonumber(a.Text), players:FindFirstChild(c.Text))

As you can see I added a “c.Text” and an “a.Text,” as you may know “a.Text” is the amount Textbox. “c.Text” is an extra thing I added, so you can pick any player in the server to give the currency to. When creating this we have to define all of the players in the server and see if there is a name that matches with the text typed into the textbox. something like this would work:

local players = game:GetService("Players")
local enter = script.Parent.Parent.enter
local d = false
local event = game.ReplicatedStorage.FatbuxP
local a = script.Parent
local c = script.Parent.Parent.TextBox2
	enter.MouseButton1Click:Connect(function()
        if players:FindFirstChild(c.Text) then -- make sure that player exists
			if d == false then -- debounce
				d = true
                -- code here
                d = false
                event:FireServer(tonumber(a.Text), players:FindFirstChild(c.Text)) -- sends the data to the server
            end
        else
            -- code here
            d = false
        end
    end)

Changing The Data

Now that you have finished everything GUI wise, you are ready to finish up and give the player their currency. First thing is we have to make sure we correctly add in the data that was sent to the server, then we can finish off by finally giving the player their currency:

game.ReplicatedStorage.FatbuxP.OnServerEvent:Connect(function(player, amt, cmt) -- amt is the number and cmt is the player
	if type(amt) ~= "number" then -- checks if it is really a number
		print("not a number")
		return
	end
	wait(1)
    cmt.Fatbux.Value = cmt.Fatbux.Value + amt -- adds the amount the player asked for to their currency
end)

At the end it should be similar to this little clip:

Extra Information

Pictures Of Whereabouts

Fat Simulator 🎄CHRISTMAS UPDATE🎄! - Roblox Studio 12_27_2019 10_53_26 PM

Fat Simulator 🎄CHRISTMAS UPDATE🎄! - Roblox Studio 12_27_2019 10_53_38 PM

Extra stuff I added

1.) I have worked on a cash effect and suffix list for the currency display cash effect:(How To Make A Cool Cash Effect Script In Roblox - YouTube) suffix: (Most efficient way of converting a number in scientific notation into a Suffix and back? - #3 by woot3)

2.) Used my own detail in GUI like the loading/converting effect. (You Probably Already Know Lol)

Here is a downloadable file, so you can get a better understanding of how everything is set up if you are still confused: Test.rbxl (33.6 KB)

Thanks For Reading!:grin: If you end up having any trouble with this, make sure to try and contact me, so I can help!

20 Likes

Overall this is a nice article, nice job. It’s a simple topic but it would be helpful for new developers.

Just a couple of suggestions:
Whenever you’re writing some code for an article or an open-source script, you gotta make sure that you’re script is easily comprehensive, doesn’t contain any ambiguity, and offers some code efficiency. For example in this code

game.ReplicatedStorage.FatbuxG.OnClientEvent:Connect(function()
script.Parent.Parent.Visible = true – make sure to add all of your things in the Frame and set them to either visible or not
script.Parent.Parent.giving.Visible = false
script.Parent.Visible = true
script.Parent.Parent.TextBox2.Visible = true
script.Parent.Parent.TextBox.Visible = true
script.Parent.Parent.text.Visible = true
script.Parent.Parent.enter.Visible = true
end)
script.Parent.MouseButton1Click:Connect(function() – function to close GUI if denied
script.Parent.Parent.Visible = false
end)

You can see script.Parent.Parent is mentioned a lot, and it’s common practice to put inside of a variable for more readable code. Remember that beginners will be reading code thus they’re gonna learn something from your written code, so it should have that property so they understand what you did and do the same as you did.

Also in this section

local enter = script.Parent.Parent.enter
local d = false
local event = game.ReplicatedStorage.FatbuxP
local a = script.Parent
local c = script.Parent.Parent.TextBox2

There is some redundant things that could be avoided to gain more readability. Both c and enter use script.Parent.Parent, you might not be using it somewhere else but for common practice it should be on a variable on its own. And also since script.Parent.Parent has script.Parent in it, and a is script.Parent, it should be saved as a.Parent

Another super important part is the ambiguity, meaning the variable names don’t make much sense. They should be named appropriately so the reader knows instantly what that variable is for, so you wanna name all your variables and all your parameters something logical so users can understand quickly, a and c ect… arent good names. And also don’t hesitate to add comments everywhere in your script, you seem to be doing that, but try your best to explain each part briefly.

Goodluck with your next articles!
Update: It seems that you didn’t apply the changes

6 Likes

I edited this to make a bank system, very useful, thank you!

1 Like

It worked for me, but however, I don’t lose any money when I give it to someone else.

1 Like