Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Help with adding +1 to a textbox that starts at 1?

Asked by 1 year ago

i have a shop gui, after clicking on one of the items another gui opens and displays the picture, description and the amount of how many you want to purchase with an increase and decrease button. how do i add 1 or subtract 1 from a textbox?

2 answers

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

Modifying @Andromedual's answer using @xInfinityBear's comment and my modifications

The title above says it all, except for my modifications. What are my modifications? Well first off, he didn't clarify on where should this script be placed. Secondly, he used the unknown variables blahablahla and otherblaglahalhla, which might confuse the OP.

Example & How To Do It

Let's say you have a ScreenGui in StarterGui. Inside it, there is one TextBox and two TextButtons named Up and Down. Inside the ScreenGui, create a new LocalScript. Inside the script should look like this:

01local gui = script.Parent -- StarterGui.ScreenGui
02local textBox = gui:WaitForChild("TextBox")
03local up = gui:WaitForChild("Up")
04local down = gui:WaitForChild("Down")
05 
06local value = textBox:GetAttribute("Value") -- gets the attribute
07if not value then -- if attribute wasn't created yet (nil)
08    textBox:SetAttribute("Value", 0) -- creates a new attribute
09    value = textBox:GetAttribute("Value")
10end
11textBox.Text = tostring(value) -- converts numbers to text
12 
13up.MouseButton1Down:Connect(function()
14    local currentValue = textBox:GetAttribute("Value")
15    textBox:SetAttribute("Value", (currentValue + 1)) -- adds 1 to the attribute
View all 29 lines...

WARNING: DO NOT COPY-PASTE THIS DIRECTLY TO YOUR CODE AS IT MIGHT NOT FUNCTION INTENDEDLY. THIS SOLUTION JUST GIVES YOU AN IDEA OF WHAT TO DO. IMPLEMENT THIS YOURSELF.

1
Thanks, works perfectly. I've never used the attribute stuff. SkrBolSp 15 — 1y
Ad
Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

From what I understand, you want to make a textbox that will change from 1, 2, 3, or downwards like 3, 2, and 1 from what you press. I believe a good solution to do this would to create a local value inside your script, and make it +1 or -1 based on the button you press, like this?

01local value = 0
02blahablahla.Activated:Connect(function()
03    value += 1
04end)
05 
06otherblaglahalhla.Activated:Connect(function()
07    if value > 0 then
08        value -= 1
09    end
10end)
11 
12value.Changed:Connect(function()
13    blahblahtextlabel.Text = value
14end)

I haven't tested this but it should work. if it doesn't work experiment with it for a bit. if you need to add other things inside the text label, try

1blahblahtextlabel.Text = ("Buying: " .. value)
1
The Changed event is a member of Instance; using it on a number will result in an error. Use an Attribute along with the AttributeChanged event or use GetAttributeChangedSignal to detect when it is changed. xInfinityBear 1777 — 1y

Answer this question