Compairing string to number in a textbox

so i have a textbox script that compairs if the text the player entered is smaller or equal to their vlue, but it keep saying “attempt to compare string <= number”, how can i fix this?

local number = button.Parent.TextBox.Text
		if number <= player.Resources.Wood.Value then

Try:

if tonumber(number) <= player.Resources.Wood.Value then
1 Like

hmm thats werid, its still giving me the same error

Make sure number is just the number, not like “19 wood” or smth like that.

@Red_NightBlade has the right code. Here’s an example code using his line.

if tonumber(number) <= player.Resources.Wood.Value then
    print(player.Name.." has "..tonumber(number).." wood planks!")
end

i have a script that only allows numbers so pretty sure thats not the problem

local TextBox = script.Parent
TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	TextBox.Text = TextBox.Text:gsub('%D+', '');
end)

What’s the default text for the TextBox?

nothing, if you meant placeholder text, its this:
“Insert number here…”

Can you print the text? Would be helpful.

like print the number? it printed what i wrote

local number = button.Parent.TextBox.Text
print(number)

" 19:39:17.278 69999 - Client - LocalScript:7
19:39:17.278 Players.foxnoobkite.PlayerGui.Drop.Frame.TextButton.LocalScript:8: attempt to compare string <= number "

I am not sure about this problem, I’ll look into it more.

1 Like

Now do print(type(number)), you’ll notice that it prints “string”, as number points to a string value not a number value.

Use tonumber() in the comparison operation itself.

if tonumber(number) <= player.Resources.Wood.Value then

oh now it says "attempt to compare number <= string " lol
(also when i didn’t type anything and typed enter it says attempt to compair nil)

To check if the string is nil or not you can do

if (string.len(Textbox.Text) >= 1) then
--// Code
end

Also for the error:

“attempt to compare number <= string”

You just got to make sure that both values are numbers by using tonumber on both like so!

if (tonumber(number) <= tonumber(player.Resources.Wood.Value)) then
--// Code
end
1 Like
if tonumber(number) <= tonumber(player.Resources.Wood.Value) then
1 Like

Don’t use if (true) then this isn’t JS

You can delete the comment I fixed it lmao also you can use

if (true) then

end

ej:

local Textbox = PlayerGui.ScreenGui:FindFirstChild("Textbox")

if (Textbox) then

end

is there a way to make this script better? since i gotta write like 20 of theres bc theres 20 different materials

if button.Parent.Label.Text == "Dropping: Wood" then
		local number = button.Parent.TextBox.Text
		if tonumber(number) <= tonumber(player.Resources.Wood.Value) then
			print("yesss")
			--drop:FireServer("Wood", number)
		elseif tonumber(number) >= tonumber(player.Resources.Wood.Value) then
			button.TextColor3 = Color3.new(0.985946, 0, 0.0406043)
			button.Text = "INVAILD NUMBER"
			wait(1)
			button.TextColor3 = Color3.new(255,255,255)
			button.Text = "Drop"
	end 
end

You can make a function

ej:

function DropMaterial(Material)
    local number = button.Parent.TextBox.Text
	if tonumber(number) <= tonumber(player.Resources:FindFirstChild(Material).Value) then
		print("yesss")
		--drop:FireServer(Material, number)
	elseif tonumber(number) >= tonumber(player.Resources:FindFirstChild(Material).Value) then
		button.TextColor3 = Color3.new(0.985946, 0, 0.0406043)
		button.Text = "INVAILD NUMBER"
		wait(1)
		button.TextColor3 = Color3.new(255,255,255)
		button.Text = "Drop"
	end 
end

Since I don’t know your full code and how you use it I can’t help you much, but if you need more help hit me up on disc

Onoille#2256

oh it is in a function , heres the full script

local button = script.Parent
local drop = game.ReplicatedStorage.Dropping
local player = game.Players.LocalPlayer
button.MouseButton1Click:Connect(function()
if button.Parent.Label.Text == "Dropping: Wood" then
		local number = button.Parent.TextBox.Text
		if tonumber(number) <= tonumber(player.Resources.Wood.Value) then
			print("yesss")
			--drop:FireServer("Wood", number)
		elseif tonumber(number) >= tonumber(player.Resources.Wood.Value) then
			button.TextColor3 = Color3.new(0.985946, 0, 0.0406043)
			button.Text = "INVAILD NUMBER"
			wait(2)
			button.TextColor3 = Color3.new(255,255,255)
			button.Text = "Drop"
	end 
end

end)

Dm me in discord or give me yours