How Would You Set A Value From A TextBox?

I have a special GUI that gives you a currency for your choice of an amount, but I can not quite figure out how to actually give them the amount they want. I would just like a nice clear understanding of how I could set a Value from the text of a TextBox. So far I have it set up, so you can only type in numbers in the TextBox and other features to make the GUI look nice. I have looked online and the dev forum for answers, but I did not quite understand and I could not find anything closely related to my problem. Examples would be great to provide, but please do not give me answers, for I really want to learn how to do this.

(If you would like to see my script tell me and I will add it)

Thanks For Reading And Maybe Helping!:grin:

8 Likes

I don’t think I understand what you’re asking, can’t you just set the value to TextButton.Text? If you want to use a NumberValue, I guess you can convert it to a number using tonumber, but I’m really not sure what part you’re stuck by.

4 Likes

What you can do is have a TextButton hooked up to a MouseButton1Click event that’ll fire the RemoteEvent you’re using to add money.

Here’s a quick snippet.

local button = script.Parent
local moneyEvent = PATH_TO_REMOTE --replace with path to money event
local box = PATH_TO_TEXTBOX

button.MouseButton1Click:Connect(function()
    moneyEvent:FireServer(tonumber(box.Text))
end)

You would use tonumber() to ensure that the box’s input is a number and not some random text value.

4 Likes

ok I used tonumber after the remote was passed and it gave me this error:
attempt to perform arithmetic on local ‘amount’ (a nil value)

I am going to try your example now, but I guess that means I have to fire the text using tonumber with a remote? :man_shrugging:

(this was before the topic was created)

2 Likes

If I’m understanding you correctly, you’re asking if you need to fire the text from the TextBox using tonumber with a RemoteEvent – and the answer is yes. You should be using tonumber to make sure that your input is a number.

Make sure you implement sanity checks though, like making sure the text isn’t nil and is a number.

1 Like

nvm sorry I just realized that was a userdata problem, when using the OnServerEvent Event, how would you set up the textbox input? I am a little confused because this is a userdata problem:
game.ReplicatedStorage.Pick.OnServerEvent:Connect(function(player, a)

1 Like

I am stuck on the part for receiving the event, I can not figure it out and it just gives me a userdata problem

1 Like

So I have a problem where my text is a number string in the textbox, but when I use tonumber it shows up as string in the Output for type. I am confused, do you know why it is doing this?

1 Like

Hate to break it to you but that is what the OP wants at all.

@Hello42bacon
something like this

 local box = script.Parent
 local event  = game.ReplicatedStorage.any_event

 box.FocusLost:Connect(function()--when the user stops inputting text
     local text = box.Tex  
     event:FireServer(text)--we get the number and send that value to the Server
     print("event was fired ")
 end)   

Now utilize that RemoteEvent

local Event = game.ReplicatedStorage.any_event

Event.OnServerEvent:Connect(function(Player, amt)
	if type(amt) == "number" then--make sure it's a number
        Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + amt	
        print("Cash was added !!")
    end
end)

Note that this system exists solely for demonstration, it can be exploited quite heavily.

10 Likes

Didn’t realize FocusLost existed, haha

Nevertheless, when dealing with money, it may be better to use a TextButton to ensure that the player is 100% sure about their desired input.

1 Like

yes I used a textbutton for this, FocusLost was just an example he gave me.

2 Likes