Putting variables into text

Hello, I have not seen any posts on the DevForum about this so I am not sure if Roblox Lua supports this.

What I want to do is when a button is pressed it sends a number to a variable, that variable will then be displayed onto a TextGui t̶h̶a̶t̶ ̶i̶s̶ ̶r̶u̶n̶n̶i̶n̶g̶ ̶a̶ ̶W̶h̶i̶l̶e̶T̶r̶u̶e̶D̶o̶ ̶l̶o̶o̶p̶ ̶t̶o̶ ̶k̶e̶e̶p̶ ̶c̶h̶e̶c̶k̶i̶n̶g̶ ̶t̶h̶e̶ ̶v̶a̶r̶i̶a̶b̶l̶e̶ ̶u̶n̶t̶i̶l̶ ̶s̶o̶m̶e̶t̶h̶i̶n̶g̶ ̶c̶o̶m̶e̶s̶ ̶u̶p̶.̶

However, the thing I want is something that displays a variable to a TextGui. It could be string, number, etc.

Can this be done?

(This variable would be a physical object and not just in the script)

It’s hard to tell but this is a cash register.

What I am trying to do is that when a number is pressed it’ll go into a physical variable, when the PLU key is pressed it will change another variable (complicated, I know but I am a newbie at scripting and couldn’t figure out another way). When that variable is enabled it will allow a new number to be added to that old number.

  1. Press number (Put into variable)
  2. Press PLU (Adding enabled)
  3. Press number (Add onto variable)
  4. Two numbers added + new number put into variable
  5. Variable displayed (in a TextLabel)
3 Likes

Why use a while loop when you can just use the Changed event with a NumberValue? Can you show some code to clarify your question a bit?

2 Likes

You don’t need a while loop; simply update the text whenever the MouseButton1Click event fires for the button instead.

1 Like

Hi - I am not used to events as I am more a newbie at scripting this type of stuff therefore I am a bit confused with them.

1 Like

The way you’re wording your question is extremely confusing but yes, you can have the values of variables represented in text (Gui elements that can display text, e.g. TextLabels) so long as they can be ran through tostring and return a string (numbers, strings, tables with a __tostring metamethod, etc) without using a physical object.

I do not advise you use a loop at any point here. You can settle for an event-driven system where you fire an event when the variable updates (assuming you only ever change it one way) which then you can update the text property to the value of the variable.

3 Likes

As I have said before, I have never seen this question on the DevForum so therefore have no idea how this would function in practice.

Are you able to link the basic information I would need to do this?

1 Like

The MouseButton1Click event fires for Gui button objects, e.g when you click a text button .

You can use a code similar to this and connect the function to run every time the button fires.

   local button = script.Parent -- the Text button instance
   local label = script.Parent.Parent.TextLabel

   button.MouseButton1Click:Connect(function()
       label.Text = --whatever you want
   end)

or you could do :

  local label = script.Parent.Parent.TextLabel -- text label
  local text = "myText"
  
  local function OnButtonClicked()
        label.Text = text
        print(label.Text) -- the same as what the text variable contained
  end

  local button = script.Parent -- TextButton
  
  button.MouseButton1Click:Connect(OnButtonClicked)
1 Like

You can do your own research from the Developer Hub. There are a couple of threads around here on the DevForum about putting things in variables into text but for more specific systems so you’ll actually have to search those threads out as opposed to just “variable in text”. For example, displaying cash.

2 Likes

It’s hard to tell but this is a cash register.

What I am trying to do is that when a number is pressed it’ll go into a physical variable, when the PLU key is pressed it will change another variable (complicated, I know but I am a newbie at scripting and couldn’t figure out another way). When that variable is enabled it will allow a new number to be added to that old number.

  1. Press number (Put into variable)
  2. Press PLU (Adding enabled)
  3. Press number (Add onto variable)
  4. Two numbers added + new number put into variable
  5. Variable displayed

(this really should have been in my beginning statement so I’ve added that now)

Ok so pretty much when you click a number, it gets added to the end of the previous number
(ex. you press 1 then 2 then 3 then 4. the final number is 1234)

Is this what you are trying to accomplish?

Ok I did some research regarding PLUs. That helped to clear up what you are trying to do
PLU stands for Price look-up

So what happens is you put in the number ID of an item (for instance 1234), then you press PLU. What happens after is the the item with the ID (1234)'s price is added to the cash register.
(Cash Registers Jargon Buster - What Is A PLU? - YouTube Video that helped me out here)

Anyways so how can we do this?
First you will want to have a table with IDs and prices so that you can look it up them later

local ShopItems = {["1"] = 20, ["2"] = 40, ["3"] = 25}

Now we want to log the buttons
I will do this in a UI, however you can replicate this in the cash register later
image
image
What I have done is put all the buttons under one parent, which you could group them all
now continuing the code

local buttonsParent = script.Parent.Frame
for i,v in ipairs(buttonsParent:GetChildren()) do
   if v:IsA("TextButton") then
      v.MouseButton1Click:Connect(function()

      end)
   end
end

Your code may look something like this

local buttonsParent = script.Parent.ButtonGroup
for i,v in ipairs(buttonsParent:GetChildren()) do
   if v:FindFirstChild("ClickDetector") then
      v.MouseClick:Connect(function()

      end)
   end
end

Now we have to code ID creation, I will create a new variable. What we will do is add the number to the variable as we go on

local buttonsParent = script.Parent.Frame
local tempId = ""
for i,v in ipairs(buttonsParent:GetChildren()) do
   if v:IsA("TextButton") then
      v.MouseButton1Click:Connect(function()
         tempId = tempId.. v.Name --Make sure you name each button to it's Value
      end)
   end
end

So far so good!
Now to code the PLU button. What we will do is get the value from the table of Ids and add it to a variable called total cost!

local ShopItems = {["1"] = 20, ["2"] = 40, ["3"] = 25, ["11"] = 110}
local buttonsParent = script.Parent.Frame
local tempId = ""
local totalCost = 0

for i,v in ipairs(buttonsParent:GetChildren()) do
   if v:IsA("TextButton") then
      v.MouseButton1Click:Connect(function()
         tempId = tempId.. v.Name 
		 print(tempId)
      end)
   end
end

script.Parent.PLU.MouseButton1Click:Connect(function()
   if ShopItems[tempId] then --Makes sure that the ID is in the table
      totalCost = totalCost + ShopItems[tempId] 
	  print("Cost = "..totalCost)
   end
   tempId = "" --Resets the Id to be used again!
end)

So big takeaways
You can add strings/numbers to the end of a variable with 2 dots (. .)

local var = "Hello"
var = var.." World ".. 123
print(var) -- Prints Hello World 123

Here is the file I made for the testing, GL!
Cash Register Test.rbxl (23.0 KB)

2 Likes

If you still need help with this feel free to reach out to me on discord @ kingerman88#4735