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:

local gui = script.Parent -- StarterGui.ScreenGui
local textBox = gui:WaitForChild("TextBox")
local up = gui:WaitForChild("Up")
local down = gui:WaitForChild("Down")

local value = textBox:GetAttribute("Value") -- gets the attribute
if not value then -- if attribute wasn't created yet (nil)
    textBox:SetAttribute("Value", 0) -- creates a new attribute
    value = textBox:GetAttribute("Value")
end
textBox.Text = tostring(value) -- converts numbers to text

up.MouseButton1Down:Connect(function()
    local currentValue = textBox:GetAttribute("Value")
    textBox:SetAttribute("Value", (currentValue + 1)) -- adds 1 to the attribute
end)

down.MouseButton1Down:Connect(function()
    local currentValue = textBox:GetAttribute("Value")
    if currentValue > 0 then
        textBox:SetAttribute("Value", (currentValue + 1)) -- subtracts 1 from the attribute
    end
end)

textBox:GetAttributeChangedSignal("Value"):Connect(function() -- when the attribute changes
    local currentValue = textBox:GetAttribute("Value")
    textBox.Text = tostring(currentValue)
    textBox:SetAttribute("Value", math.clamp(currentValue, 0, math.huge)) --  if currentValue is less than 0 (min) it will change the attribute to 0
end)

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?

local value = 0
blahablahla.Activated:Connect(function()
    value += 1
end)

otherblaglahalhla.Activated:Connect(function()
    if value > 0 then
        value -= 1
    end
end)

value.Changed:Connect(function()
    blahblahtextlabel.Text = value
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

blahblahtextlabel.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