Why does it print out "nil" while im trying to use tonumber()

so basically its a random number generator, except, a player can change its values in a textbox, i tried using tonumber() to convert my text to number, but it doesnt seem to work, i tried to see what it prints out and heres the result.
image

local StartNum = script.Parent:FindFirstChild("Start")
local EndNum = script.Parent:FindFirstChild("End")
local Guess = script.Parent:FindFirstChild("Opinion")
local Number = script.Parent:FindFirstChild("Number")
local Click = script.Parent:FindFirstChild("TextButton")

Click.MouseButton1Click:Connect(function()
	local StartStr = tonumber(StartNum.Text)
	print(StartStr)
	local EndStr = tonumber(EndNum.Text)
	print(EndStr)
	local GuessStr = tonumber(Guess.Text)
	print(GuessStr)
	if not StartStr or EndStr or GuessStr then return end
	local num = math.random(StartStr,EndStr)
	if num == GuessStr then
		Number.Text = num
		Number.TextColor3 = Color3.fromRGB(0,255,0)
	else
		Number.Text = num
		Number.TextColor3 = Color3.fromRGB(255,0,0)
	end
end)

Try Printing what Start, End, Opinion etc. are in the MouseButton1Click function so you can see if the issue is in locating the values of children of script.parent.
If you are finding Values, I think you should be using Start.Value, End.Value etc.

i tried printing it out and, uhh, its just empty (i did put random numbers in the textboxes)
image
image

The roblox documentation says, “To convert a string to a number, use the tonumber() function. If the string doesn’t have a number representation, then the tonumber() function returns [nil]”(Nil | Roblox Creator Documentation).

You could try using a TextBox Mask, which can help you remove unwanted stuff from your text.

function PositiveIntegerMask(text)
	return text:gsub("[^%-%d]", "")
end

Full Example:

function PositiveIntegerMask(text)
	return text:gsub("[^%-%d]", "")
end

Click.MouseButton1Click:Connect(function()
	local StartStr = PositiveIntegerMask(StartNum.Text)
	print(StartStr)
	local EndStr = PositiveIntegerMask(EndNum.Text)
	print(EndStr)
	local GuessStr = PositiveIntegerMask(Guess.Text)
	print(GuessStr)
	if not StartStr or EndStr or GuessStr then return end
	local num = math.random(StartStr,EndStr)
	if num == GuessStr then
		Number.Text = num
		Number.TextColor3 = Color3.fromRGB(0,255,0)
	else
		Number.Text = num
		Number.TextColor3 = Color3.fromRGB(255,0,0)
	end
end)

But what is the variable Start?
Is it a Value, orthe text box input, or the number of times a player starts playing a round?
You may be getting an incorrect answer because the item you are storing as the variable doesn’t fit the parameters you are trying to read in this section of script.

Changes made on the client, such as typing into a text box, aren’t replicated to the server. You need to use a RemoteEvent for that.

Here’s an example if you had a client and server scipt. (This may not be the best piece of code and is just made to be an* example)

-- Client
local remoteEvent = "your remote event here"

local StartNum = script.Parent:FindFirstChild("Start")
local EndNum = script.Parent:FindFirstChild("End")
local Guess = script.Parent:FindFirstChild("Opinion")
local Click = script.Parent:FindFirstChild("TextButton")

function PositiveIntegerMask(text) -- Removes anything that isnt a number from text
	return text:gsub("[^%-%d]", "")
end

Click.MouseButton1Click:Connect(function()
	local StartStr = PositiveIntegerMask(StartNum.Text)
	local EndStr = PositiveIntegerMask(EndNum.Text)
	local GuessStr = PositiveIntegerMask(Guess.Text)
	
	if #StartStr < 1 or #EndStr < 1 or #GuessStr < 1 then print("Please fill out the textboxes | Only numbers accepted") return end -- Check if there is more than one NUMBER in the text
	
	remoteEvent:FireServer(StartStr, EndStr, tonumber(GuessStr)) -- tonumber() is done here because it is later compared with another number, you can't compare number and string
end)



-- Server
local remoteEvent = "your remote event here"

--local Number = script.Parent:FindFirstChild("Number")

remoteEvent.OnServerEvent:Connect(function(player, Start, End, Guess)
	local num = math.random(Start, End) -- Random number between Start Value and End Value
	--Number.Text = num
	
	if num == Guess then
		print("Correct")
		-- Number.TextColor3 = Color3.fromRGB(0,255,0)
	else
		print("Incorrect")
		-- Number.TextColor3 = Color3.fromRGB(255,0,0)
	end
end)
2 Likes