Problem with StringValue and Number Conversion

Hello,

I’m trying to make a TextButton (RandomInputAdd) increase in numerical value by 1 increment per click. All is going well - every time I click the button, it increases - but the problem has arisen with my conditional statement (if RandomInputAdd.Text > 10 then RandomInputAdd.Text = 1). With this statement, I want it so that when the value of the text exceeds 10, it will reset back to 1.

Note that Chosen is a StringValue, and, since Lua can’t perform a comparison between that and a number, I need a way to convert it into a number somehow…

I’ve tried performing tonumber() functions, adding an IntValue to the TextButton, but I’m not too well knowledgable in this area.

local StarterGui = game:GetService("StarterGui")
local GUI = script.Parent

local RandomInputAdd = GUI.Frame.RandomInputAdd

local Possible = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

function Choose()
	local IndexAdd = math.random(1, #Possible)
	local ChosenAdd = Possible[IndexAdd]
	RandomInputAdd.Text = ChosenAdd
	print(ChosenAdd)
end

Choose()

RandomInputAdd.MouseButton1Click:Connect(function()
	RandomInputAdd.Text = RandomInputAdd.Text + 1
	if RandomInputAdd.Text > 10 then
		RandomInputAdd.Text = 1
	end
end)

u can’t use + 1 in a string instead do text = “text you want”…tostring(a value)
and if u want to make a string to a number use tonumber(value)

Is it possible for you to make up an example? I’ve been struggling to put it into my script for a while.

if tonumber(RandomInputAdd.Text) > 10 then
    RandomInputAdd.Text = 1
end

You could also maintain a numerical copy and use that instead. (I recommend this)

1 Like