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:
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.
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)