How to check if something is a number?

Basically I wanted to add something like a coinflip and use a betamount function, so basically someone enters the amount of chips they want to bet and then they flip and they regain it back by 2 or lose it or get a tie, but if I enter a word instead of a number in the textbox it bugs and I can’t use the textbox again. How do I fix it and check if something is a number? This is the script I currently have:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local buttonpressed = false
local betamount = tonumber(script.Parent.Parent.BetAmount.Text)
script.Parent.MouseButton1Click:Connect(function()
	if buttonpressed then return end
	if not buttonpressed then
	buttonpressed = true
	local dicething = script.Parent.Parent
		local plr = game.Players.LocalPlayer
		if plr.leaderstats.Chips.Value >= betamount then
		plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value -betamount
	dicething.YourNumber.NumberRolled.Value = math.random(1, 12)
	dicething.TheirNumber.NumberRolled.Value = math.random(1, 12)

	script.Parent.Parent.YourNumber.Text = tostring(dicething.YourNumber.NumberRolled.Value)
	script.Parent.Parent.TheirNumber.Text = tostring(dicething.TheirNumber.NumberRolled.Value)
	if dicething.TheirNumber.NumberRolled.Value > dicething.YourNumber.NumberRolled.Value then
	dicething.Status.Text = "You lost!"
	elseif dicething.TheirNumber.NumberRolled.Value < dicething.YourNumber.NumberRolled.Value then
	dicething.Status.Text = "You won!"
	plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value + betamount*2
	elseif dicething.TheirNumber.NumberRolled.Value == dicething.YourNumber.NumberRolled.Value then
	dicething.Status.Text = "Tie! GG"
	plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value + betamount
	end
	elseif plr.leaderstats.Chips.Value < betamount then
	end
	end
	wait(3)
	buttonpressed = false
end)	

I don’t know if there is a function like IsANumber or something but if there is please tell me how to use it.

You can accomplish that by taking the input and using the tonumber() function on it, with the base being 10. For example tonumber(n, 10). If it returns nil then it’s not an integer. Since the value will be entered as a string anyway from the input in most cases.

1 Like

how and were should I implement that in the code?

You can check if betamount is nil along with the buttonpressed check. For example:

if not buttonpressed and betamount then

That will check if the button isn’t already pressed, and will also make sure that betamount isn’t false or nil.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local buttonpressed = false
local inputtext = script.Parent.Parent.BetAmount.Text
local betamount = tonumber(inputtext, 10)
script.Parent.MouseButton1Click:Connect(function()
	if buttonpressed then return end
	if not buttonpressed and betamount then
		buttonpressed = true
		print("working")

So it doesn’t print working so that means there is an error there, but what?

You can make a separate check underneath the buttonpressed check, which would give you a response if the betamount isn’t a number. Like this:

if not buttonpressed then
    if betamount == nil then
         -- Output some error to the user
         return
    end
    -- Do your code
end

Players.seth20062.PlayerGui.Dice.ScreenGui.Frame.PlayButton.LocalScript:15: attempt to compare nil and number

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local buttonpressed = false
local inputtext = script.Parent.Parent.BetAmount.Text
local betamount = tonumber(inputtext. 10)
script.Parent.MouseButton1Click:Connect(function()
	if buttonpressed then return end
	if not buttonpressed then
		if betamount ~= nil then
			print("BetAmount is nill")
		end
		buttonpressed = true
		print("working")
	local dicething = script.Parent.Parent
		local plr = game.Players.LocalPlayer
		if plr.leaderstats.Chips.Value >= betamount then
		plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value -betamount
	dicething.YourNumber.NumberRolled.Value = math.random(1, 12)
	dicething.TheirNumber.NumberRolled.Value = math.random(1, 12)

	script.Parent.Parent.YourNumber.Text = tostring(dicething.YourNumber.NumberRolled.Value)
	script.Parent.Parent.TheirNumber.Text = tostring(dicething.TheirNumber.NumberRolled.Value)
	if dicething.TheirNumber.NumberRolled.Value > dicething.YourNumber.NumberRolled.Value then
	dicething.Status.Text = "You lost!"
	elseif dicething.TheirNumber.NumberRolled.Value < dicething.YourNumber.NumberRolled.Value then
	dicething.Status.Text = "You won!"
	plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value + betamount*2
	elseif dicething.TheirNumber.NumberRolled.Value == dicething.YourNumber.NumberRolled.Value then
	dicething.Status.Text = "Tie! GG"
	plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value + betamount
	end
	elseif plr.leaderstats.Chips.Value < betamount then
	end
	end
	wait(3)
	buttonpressed = false
end)	

This is the whole code btw

Fixed a typo in the code. Check it now, and make sure to include the return so the function doesn’t go on after it.

what typo? Maybe im just blind lol but what typo?

It was in my code, no worries. The check in the if statement was checking if it is not nil, not if it is nil. Which would inverse the function. Here’s the code after fixing it:

if not buttonpressed then
    if betamount == nil then
         -- Output some error to the user
         return
    end
    -- Do your code
end

To check if something is a number, you can use type().

e.g.

if type(betamount) == "number" then
    --code
else
    return
end
1 Like
betamount:GetPropertyChangedSignal("Text"):Connect(function() 
	if type(betamount.Text) == "number" then		
	else		
		betamount.Text = betamount.Text:gsub('%D+', '')	
	end
end)

okay ill try that, I hope it works

I tried it but idk how to implement it

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local buttonpressed = false
local inputtext = script.Parent.Parent.BetAmount.Text
local betamount = tonumber(inputtext)
script.Parent.MouseButton1Click:Connect(function()
	if buttonpressed then return end
	if not buttonpressed then
		betamount:GetPropertyChangedSignal("Text"):Connect(function()
			if type(betamount.Text) == "number" then
		buttonpressed = true
		print("working")
	local dicething = script.Parent.Parent
		local plr = game.Players.LocalPlayer
		if plr.leaderstats.Chips.Value >= betamount then
		plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value -betamount
	dicething.YourNumber.NumberRolled.Value = math.random(1, 12)
	dicething.TheirNumber.NumberRolled.Value = math.random(1, 12)

	script.Parent.Parent.YourNumber.Text = tostring(dicething.YourNumber.NumberRolled.Value)
	script.Parent.Parent.TheirNumber.Text = tostring(dicething.TheirNumber.NumberRolled.Value)
	if dicething.TheirNumber.NumberRolled.Value > dicething.YourNumber.NumberRolled.Value then
	dicething.Status.Text = "You lost!"
	elseif dicething.TheirNumber.NumberRolled.Value < dicething.YourNumber.NumberRolled.Value then
	dicething.Status.Text = "You won!"
	plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value + betamount*2
	elseif dicething.TheirNumber.NumberRolled.Value == dicething.YourNumber.NumberRolled.Value then
	dicething.Status.Text = "Tie! GG"
	plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value + betamount
	end
	elseif plr.leaderstats.Chips.Value < betamount then return
				end
			else
				betamount.Text = betamount.Text:gsub('%XD+', '')
			end
	wait(3)
			buttonpressed = false
end

error:

 Players.seth20062.PlayerGui.Dice.ScreenGui.Frame.PlayButton.LocalScript:37: Expected ')' (to close '(' at line 8), got <eof>

I saw that would not work :laughing:

then tell me what to fix and how to fix lol

Alright. This error means there’s something wrong with your code. You forgot to put an ‘)’ closing a statement somewhere or something.
Here’s a simple answer:

type(i)

Returns the type of a value in a string. String are: “number”, “string”, “userdata”, “table”.
So, whenever you need to check, do:

if type(3486338) == "number" then do_something() end
if type({}) == "table" then do_something() end
if type('str') == "string" then do_something() end
if type(newproxy(true)) == "userdata" then do_something() end

It also returns userdata if the object is an Instance. If you use typeof then it works for more values such as Vector3, UDim, UDim2 and CFrame.

Don’t use type, Instead use typeof because type isn’t always reliable sometimes returning UserData.

I was mererly explaining it, and also said that typeof returns more values.