Check if TextBox.Text String only contains numbers

Hi, i’m trying to check if a TextBox.Text string only contains numbers… Can anyone help me with this?

script.Parent.MouseButton1Click:Connect(function()
	local Player = script.parent.parent.parent.parent.parent.parent.parent.parent
	local Amount = Player.PlayerGui.Menus.Vault.Main.Holder.Amount.TextBox.Text
	if Amount... then
		
	else
		
	end
end)

My script is above.
I tryed using tonumber() but that didn’t work.

You can use string.match to do this.

if (string:match("%W")) then
    --
end

The code above matches the string against all non-alphanumeric characters.

Although, tonumber should work it will either return nil or the number.

i added that

script.Parent.MouseButton1Click:Connect(function()
	local Player = script.parent.parent.parent.parent.parent.parent.parent.parent
	local Amount = Player.PlayerGui.Menus.Vault.Main.Holder.Amount.TextBox.Text
	if (Amount:match("%W")) then
		print("1")
	else
		print("2")
	end
end)

but no matter what it just prints 2 if its all numbers, numbers and letters or just letters

What text are you writing in the text box?

“69”, “gg”, “d3dk2” is what i entered all printed 2

The best approach would be tonumber, how did you set out the code with tonumber? I’ve just tried this and it worked:

script.Parent.Activated:Connect(function()
	local Amount = script.Parent.Parent.TextBox.Text
	
	if tonumber(Amount) then
		print(Amount)
	else
		return 
	end
end)
script.Parent.MouseButton1Click:Connect(function()
	local Player = script.parent.parent.parent.parent.parent.parent.parent.parent
	local Amount = Player.PlayerGui.Menus.Vault.Main.Holder.Amount.TextBox.Text
	if tonumber(Amount) then
		print(Amount)
	else
		print("no")
	end
end)

still prints “no” and btw its serversided idk if that makes a difference
this is the same way i tryed it before

The text from the client doesn’t replicate to the server, use a local script instead.

i can’t use a local script, im making a “bank system” where you can store leaderstats, thats why im getting PlayerGui instead of StarterGui

Use a RemoteEvent to send data to the server.

that can easily be exploited though right?

wait i could prevent that if i check the player leaderstats on the server and see if its below that right tho?

1 Like

You can create checks on the server.

alright thanks bro, ill do that :slightly_smiling_face:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.