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