How to turn a string into a value?

I have a lil script that you gotta guess a random number and it keeps saying attempt to compare number > string and idk why

local number = math.random(1,10)
local view = script.Parent.HighOrLow
local box = script.Parent.TextBox

print(number)
while true do
	wait (0.05)
	if box.Text == number then
		view = (":)")
	elseif box.Text >  number then
		view.Text = ("Lower")
	elseif box.Text < number then
		view.Text = ("Higher")
	end
end

So how do I turn the string to a value btw view is a text label

tonumber() Returns a number value.

1 Like

tonumber() and tostring() exist to turn things into strings or numbers

tonumber() could turn any base into decimal using the second parameter

1 Like

How would I use that in the script tho

if tonumber(box.Text) == number then

but really should be a variable:
local boxText = tonumber(box.text)
if boxText == number then

1 Like
if tonumber(box.Text) == number then

That should work

1 Like
print(tonumber("1010"))
print(tonumber("1010", 2))

just as an example this prints

1010 for the first one
and 10 for the second one

1 Like

This issue keeps popping up idk why

what did you do?
could you show your code


local number = math.random(1,10)
local view = script.Parent.HighOrLow
local box = script.Parent.TextBox

print(number)
while true do
	wait(0.05)
	if tonumber(box.Text) == number then
		view.Text = (":)")
		elseif tonumber(box.Text) > number then
			view.Text = ("Lower")
			if tonumber(box.Text) < number then
				view.Text = ("Higher")
			end
		end
	end

tonumber(box.Text) is nil

which means box.Text can’t be a number
can you print box.Text?

1 Like

When I print box text nothin shows up probs cuz I didn’t type anything in the box ty

local number = math.random(1,10)
local view = script.Parent.HighOrLow
local box = script.Parent.TextBox

print(number)
while true do
	wait(0.05)
	if tonumber(box.Text) == number then
		view.Text = (":)")
        if tonumber(box.Text) == nil then
            continue
        end
		elseif tonumber(box.Text) > number then
			view.Text = ("Lower")
			if tonumber(box.Text) < number then
				view.Text = ("Higher")
			end
		end
	end
end

this should prevent that type of error from happening again

unless I’m wrong and continue isn’t allowed in while loops, idk since I haven’t tested it but you could tell me if it works

1 Like

It didn’t work but I tweaked ur script and got dis

local number = math.random(1,10)
local view = script.Parent.HighOrLow
local box = script.Parent.TextBox

print(number)
while true do
	wait(0.05)
	if tonumber(box.Text) ~= nil then
		if tonumber(box.Text) == number then 
			view.Text = (":)")
				elseif tonumber(box.Text) > number then
					view.Text = ("Lower")
					elseif tonumber(box.Text) < number then
						view.Text = ("Higher")
				end
			end
		end

Tysm for ur help

thanks, now I know continue doesn’t work in while loops, good to know.
anyways happy I helped

1 Like