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)
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.
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.
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.
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)
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?
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.