Guys help me! How do I fix my code?

Text contains 1 but 1 is not a winning number so how do I fix it?
I want to detect the whole number from a given string (from TextBox.Text), and check if the string contains the specific number. Text contains 1 but 1 is not a winning number so how do I fix it?

local Text = "The winning numbers are Only: 12, 13."

if Text.find(1) then
    print("1 is a winning number")
end

you’re printing that 1 is a winning number.

also, the text will always contain 1, since they both have 1 at the start.
12, 13.

you could probably do something like this

local num = math.random(1, 2) -- replace 2 with the highest number you want to be possible. if you only want it to go upto 13, put 13.

if num == 12 then
print("Win")
elseif num == 13 then
print("Win")
else
print("No win")
end
1 Like

Sorry to say you cannot use math.random() because its not a string.

You can use this: it should turn the random number into a string

local WinningNumber = {12,13}

local number = math.random(1,20)

if table.find(WinningNumber, number) then
	print("This is the number you got:"..tostring(number).. "You won!")
else
	print("This is the number you got:".. tostring(number) .."You lost!")
end

You can enter more winning numbers in the table

Sorry to say you cannot use math.random() because its not a string.

do you want to turn 1 to a string?

local Text = "The winning numbers are Only: 12, 13."

if Text.find(tostring(1)) then
    print("1 is a winning number")
end

I want to detect the whole number from a given string, and check if the whole number is equaled to a specific number.

Here’s a fix:

local Numbers = {12, 13}
local Text = "The winning numbers are Only: "
for i, number in pairs(Numbers) do
	if #Numbers == i then
		Text ..= number .. "."
	else
		Text ..= number .. ", "
	end
end
print(Text)
local Number = 13 --Use the value you want.
local index = table.find(Numbers, Number)
if index then
    print(Numbers(index) .. " is a winning number")
end

This works similarly to @PVP_Player2373902’s design.

I did a test before posting my code, you can concatenate numbers with strings perfectly.

But my numbers are already stored as strings ://

try this

local Text = "The winning numbers are Only: 12, 13."

if Text:match(tostring(1)) then
    print("1 is a winning number")
end

It does not seem to work because it still prints it out ://

The one I made stores numbers in a table then a number, creates the string based on the table, then finds if the number exists in the table. There is no need to store them as strings with my solution.

its mostly because there is 1 in “12,13”.

Lua’s string patterns really suck, no idea why they didn’t just implement the normal regex system everyone uses

local Text = "The winning number are only: 12, 13."

local numbers = Text:split(",") -- we split the commans

for index,numberwithtext in pairs(numbers) do -- we loop through the splitted points. But a problem is that it also has text so.
    local number,times = string.gsub(numberwithtext,"%D") -- we yeet the text out
    if number == 1 then -- see if number is equal to the number we want
        print("1 is a winning number!") -- do stuff.
    end
end

Error: missing argument #3 to ‘gsub’ (string/function/table expected)

ah wait I forgot lol here

local Text = "The winning number are only: 12, 13."

local numbers = Text:split(",") -- we split the commans

for index,numberwithtext in pairs(numbers) do -- we loop through the splitted points. But a problem is that it also has text so.
    local number,times = string.gsub(numberwithtext,"%D","") -- we yeet the text out
    if number == 1 then -- see if number is equal to the number we want
        print("1 is a winning number!") -- do stuff.
    end
end

Instead of using a single string to store your winning numbers, perhaps storing them inside of a table so you can open up your available options like so:

local winningNumbers = {12, 13, 23, 57, 1987349394}
local numberToCheck = 1

if table.find(winningNumbers, numberToCheck) then
	print(numberToCheck.." is a winning number!")
else
	print(numberToCheck.." is not a winning number.")
end

You can also do more complicated operations easier without needing to hardcode items so much. Like maybe randomly generating numbers between a certain range and putting them inside a table:

local winningNumbers = {12, 13, 23, 57, 1987349394}
local numberToCheck = 1

if table.find(winningNumbers, numberToCheck) then
	print(numberToCheck.." is a winning number!")
else
	print(numberToCheck.." is not a winning number.")
end

--Create 3 randomly generated numbers between 100 and 10000 and store them inside winningNumbers
for i = 1, 3 do
	table.insert(winningNumbers, Random.new():NextInteger(100, 10000))
end
print(winningNumbers) -->randomly drawn numbers are added

Or figuring out if the numbers are odd or even:

local winningNumbers = {12, 13, 23, 57, 1987349394}
local numberToCheck = 1

if table.find(winningNumbers, numberToCheck) then
	print(numberToCheck.." is a winning number!")
else
	print(numberToCheck.." is not a winning number.")
end

--Create 3 randomly generated numbers between 100 and 10000 and store them inside winningNumbers
for i = 1, 3 do
	table.insert(winningNumbers, Random.new():NextInteger(100, 10000))
end
print(winningNumbers) -->randomly drawn numbers are added

local totalEven, totalOdd = 0, 0
for i, winningNumber in pairs(winningNumbers) do
	if winningNumber%2 == 0 then
		totalEven += 1
	else
		totalOdd += 1
	end
end

print(totalEven, totalOdd) --> prints the total even and odd numbers

What if I change the number we want to check to 12? It does not print out although it is a winning number.

local Text = "The winning number are only: 12, 13."

local numbers = Text:split(",") -- we split the commans

for index,numberwithtext in pairs(numbers) do -- we loop through the splitted points. But a problem is that it also has text so.
	local number,times = string.gsub(numberwithtext,"%D","") -- we yeet the text out
	
	if number == 1 then -- see if number is equal to the number we want
		print("1 is a winning number!") -- This should not be printed out
	end
	
	if number == 12 then -- see if number is equal to the number we want
		print("12 is a winning number!") -- This should be printed out, but it does not print
	end
	
end