im making a gui that changes the workspace gravity. the player enters something in the textbox and the workspace gravity is equal to the text in the textbox (for example if i enter 196 then the gravity is now 196)
but people could just enter random characters. so i want to detect if the text is a numerical value that could actually change the gravity
local function isNumber(x: string): boolean
return tonumber(x) ~= nil
end
if isNumber("5.5") then
print("5.5 is a number!")
end
if isNumber("25x") then
print("25x is a number!") --should not print
end
Basically if the function tonumber returns a number instead of nil, it means that the string input was numeric.
local newGravityValue = tonumber(TextBox.Text)
if newGravityValue then
-- what is inserted is fully a number and you change the gravity value
else
-- did not pass the tonumber check thus meaning it's not fully a numerical value that was inserted
end