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?
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.
Let's say you have a ScreenGui
in StarterGui
. Inside it, there is one TextBox
and two TextButton
s named Up and Down. Inside the ScreenGui
, create a new LocalScript
. Inside the script should look like this:
01 | local gui = script.Parent -- StarterGui.ScreenGui |
02 | local textBox = gui:WaitForChild( "TextBox" ) |
03 | local up = gui:WaitForChild( "Up" ) |
04 | local down = gui:WaitForChild( "Down" ) |
05 |
06 | local value = textBox:GetAttribute( "Value" ) -- gets the attribute |
07 | if not value then -- if attribute wasn't created yet (nil) |
08 | textBox:SetAttribute( "Value" , 0 ) -- creates a new attribute |
09 | value = textBox:GetAttribute( "Value" ) |
10 | end |
11 | textBox.Text = tostring (value) -- converts numbers to text |
12 |
13 | up.MouseButton 1 Down:Connect( function () |
14 | local currentValue = textBox:GetAttribute( "Value" ) |
15 | textBox:SetAttribute( "Value" , (currentValue + 1 )) -- adds 1 to the attribute |
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.
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?
01 | local value = 0 |
02 | blahablahla.Activated:Connect( function () |
03 | value + = 1 |
04 | end ) |
05 |
06 | otherblaglahalhla.Activated:Connect( function () |
07 | if value > 0 then |
08 | value - = 1 |
09 | end |
10 | end ) |
11 |
12 | value.Changed:Connect( function () |
13 | blahblahtextlabel.Text = value |
14 | end ) |
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
1 | blahblahtextlabel.Text = ( "Buying: " .. value) |