TextBox User Input, Currency Conversion | Receiving Player Input

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 imput correctly when converting the tokens.

Local Script
local player = script:FindFirstAncestorWhichIsA("Player")
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)	
	print(Amount)
	if tokens.Value >= Amount then --Conversion system
		cash.Value = cash.Value + Amount
		tokens.Value = tokens.Value - Amount

		
	end
end

confirm.MouseButton1Down:connect(Click)

Thanks in advance.

2 Likes

First of all, if you do all of this on a Local Script it will not be seen on the server, I reccommend you use a RemoteEvent to connect to the server to covert the tokens to cash.

And for the player just use game.Players.LocalPlayer

I understand this, but I wanted to see if it would receive the proper input by the player, which is doesn’t. Do you know why the input isn’t received?

Mabye try putting a small wait before changing the text again.

If this does not work @TixFoil ,

change the bottom code to:

confirm.MouseButton1Click:Connect(Click())

Does not work. The error is on line 16:

if tokens.Value >= Amount then

Error: attempt to compare string <= number

After doing this use a remoteevent to connect to the server and change this code to the server on the ServerEvent. But Remove the Tokens Child from the leaderstats and make it a IntValue instead of a StringValue

Removing Tokens from Leaderstats isn’t going to do anything, Tokens is already an IntValue, and doing the Remote and ServerEvent right now isn’t going to do anything that I am worried about. The error is on line 16 because it is not receiving the proper value created by the text box, which is my only concern.

Use tonumber(Amount) when comparing it to tokens.Value

This returns as a nil value.
attempt to compare nil <= number

Why not just use an if statement with tonumber() to determine if the input was a number? Using gsub seems a bit weird for this use case. Unless you were trying to make it so the player cannot enter letters into the textbox, then I completely understand that part. If that is the case, ignore this.

Shouldn’t this be game:GetService("Players").LocalPlayer? How do you get the Player object from the scripts descendants? Player objects don’t exist under scripts. They cannot be constructed either.

Otherwise everything sort of looks fine?

Also, not like it matters, but make that function local function Click(mouse) instead.

Yes, this is to prevent letters from being typed by the player.

I also tried this and it still does not work.

I appreciate your help.

1 Like

Solved by @Limited_Unique. Thank you.

I was online 4 days ago, I’m surprised I didn’t see this thread, anyway at least it’s solved now.

1 Like