TextBox User Input, Currency Conversion | Receiving Player Input

This is a repost.

Hello, I am in the process of creating a UI system that converts in-game tokens to cash. The system is based on a TextBox, which the player can type a number into then press a “Convert” button to convert the amount of tokens the player wrote in the TextBox. The system works, it just doesn’t receive the user’s input correctly when converting the tokens.

Local Script
local player = game.Players.LocalPlayer
local cash = player.leaderstats.Cash
local tokens = player.leaderstats.Tokens
local Amount = script.Parent.Text
local confirm = script.Parent.Parent.Confirm

script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
	script.Parent.Text = script.Parent.Text:gsub('%D+', '');  -- Forces only numerical value input
end)

function Click(mouse)	--Conversion system
	print(Amount)
	if tokens.Value >= Amount then -- Error line: attempt to compare string <= number
		cash.Value = cash.Value + Amount
		tokens.Value = tokens.Value - Amount

		
	end
end

confirm.MouseButton1Down:connect(Click)

Thanks in advance.

2 Likes

Remember that leaderstats are kept server-side and any changes made to them in local scripts will only reflect locally (to the client) and not to the whole server.

1 Like

To fix the error you mentioned however just replace the reference to “Amount” with “tonumber(Amount)” tonumber() is a built-in function which will attempt to convert some value into a number value. You can also use tointeger() which is for integer conversion specifically.

local player = game.Players.LocalPlayer
local cash = player.leaderstats.Cash
local tokens = player.leaderstats.Tokens
local Amount = script.Parent.Text
local confirm = script.Parent.Parent.Confirm

script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
	script.Parent.Text = script.Parent.Text:gsub('%D+', '');  -- Forces only numerical value input
end)

function Click(mouse)	--Conversion system
	print(Amount)
	if tonumber(Amount) then
		if tokens.Value >= tonumber(Amount) then -- Error line: attempt to compare string <= number
			cash.Value = cash.Value + Amount
			tokens.Value = tokens.Value - Amount
		end
	end
end
confirm.MouseButton1Down:connect(Click)

Yes, for now I am doing this to see if I can see results from the click function.

I have tried using the tonumber() event, but that only made the number into a nil value. I did not realise tointeger() was a thing, however, so I will give that a try.

tonumber & tointeger will both return nil if they cannot convert the input into either a number or integer respectively.

How do I solve it from returning a nil value, then?

By making sure the input is a string which can be converted into a number/integer. So 4.5 for number, 4 for integer etc.

Yes, it is only numbers I type in the text box and it still returns nil.

local Amount = script.Parent.Text

That’s because you’re only defining Amount once, as soon as the script first executes.

function Click(mouse)	--Conversion system
	Amount = script.Parent.Text
	print(Amount)
	if tonumber(Amount) then
		if tokens.Value >= tonumber(Amount) then -- Error line: attempt to compare string <= number
			cash.Value = cash.Value + Amount
			tokens.Value = tokens.Value - Amount
		end
	end
end
confirm.MouseButton1Down:connect(Click)

Try this, “Amount” gets assigned the text inside the textbox every time the confirm button is clicked.

1 Like

Yes, thank you so much! I had to add tonumber(Amount) instead of the Amount for each of the conversion parts, by the way.

Yes, you would for any arithmetic/comparison operation that it’s involved in.

1 Like

No problem though, and one last thing, use tointeger if the end result should be an integer & tonumber if you want to keep any decimal part the value has.

Ok, thank you. I will not be using decimals for any of the currency, however.