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