I’m trying to convert string.gsub to a number. And for some reason I can’t.
Here’s the code:
local value = tostring(loadingScreen.Percentage.Size.Width) -- gets the width of the gui
local str = string.gsub(value, '0,','') -- removes the 0 and ,
print(tonumber(str)) -- prints the converted str
I’ve tried an abundance of ways to fix it, but all have been unsuccessful.
What’s the issue with the code, any errors?
I tried using your code to reproduce the issue, but it actually worked for me.
Here’s the example I used:
-- I used a frame object with width 0, 100 for this example
local value = tostring(script.Parent.Size.Width) -- gets the width of the frame
print(value) -- prints "0, 100"
local str = string.gsub(value, '0,','') -- removes the 0 and ,
local converted = tonumber(str) -- converts string to number
print(typeof(str)) -- prints "string"
print(str) -- prints " 100"
print(typeof(converted)) -- prints "number"
print(converted) -- prints "100"
Try printing the local variable value and see if you get what you are expecting before you try to convert it.
The only other thing I can think of is that the gui you are pulling the width from has a scale that is not 0, causing the gsub to return nil
since you use “0,” for the gsub, you are assuming that the width (x) scale is 0. Make sure it is.
All of these are honorable solutions, but I don’t know what to put as the solution. But the one that gave me a vast image of what it should look like was, @SuperSpeedy101.