Hi there!
So I have a screenGUI is being used to purchase in game items. It comes up and works fine, that is until the player gains too much money.
Im also using a script that converts long to short currency for the leaderboard stats and it adds commas as needed (1,000) and "10K 10M 10B, K,M and B.
So this issue Im having: if the player has a comma or letter in their "Cash", the purchase script will error out as it does not know what to do with anything but numbers.
So if player has $10050 they can buy a $10 item, but $10,050 we get a nil error. Same with letters.
The conversion is done in a settings module in the workspace seen below, it works fine and makes everything look nice, only now I have this issue.
So, I can remove this and just run with numbers only and it will work, but then it just doesn't look as nice.
function module:ConvertComma(num) local x = tostring(num) if #x>=10 then local important = (#x-9) return x:sub(0,(important))..","..x:sub(important+1,important+3)..","..x:sub(important+4,important+6)..","..x:sub(important+7) elseif #x>= 7 then local important = (#x-6) return x:sub(0,(important))..","..x:sub(important+1,important+3)..","..x:sub(important+4) elseif #x>=4 then return x:sub(0,(#x-3))..","..x:sub((#x-3)+1) else return num end end function module:ConvertShort(Filter_Num) local x = tostring(Filter_Num) if #x>=10 then local important = (#x-9) return x:sub(0,(important)).."."..(x:sub(#x-7,(#x-7))).."B+" elseif #x>= 7 then local important = (#x-6) return x:sub(0,(important)).."."..(x:sub(#x-5,(#x-5))).."M+" elseif #x>=4 then return x:sub(0,(#x-3)).."."..(x:sub(#x-2,(#x-2))).."K+" else return Filter_Num end end
and here is an example of the purchasing script: (localscript)
local Settings = require(game.workspace.Settings) >>I added this after to see if it would help, but no. local plr = game.Players.LocalPlayer local backpack = game.Players.LocalPlayer.Backpack local tool = game.ReplicatedStorage.Cinnabun script.Parent.MouseButton1Click:Connect(function() if tonumber(plr.leaderstats.Cash.Value) >= 5 then if not plr:FindFirstChild("Cinnabun", true) and not plr.Character:FindFirstChild "Cinnabun" then plr.leaderstats.Cash.Value = tonumber(plr.leaderstats.Cash.Value) - 5 tool:Clone().Parent = plr.Backpack end end end)
If there are any suggestions you may have or if you have run into this issue any thought would be much appreciated!
Cheers!
One, your formatting is pretty.. messed up.
Try and use the preview function next time, meanwhile I'll assume you're having issues getting a numeric value with commas in it to work out nicely with Lua.
A recommendation is to use string patterns!
Numbers in Lua don't have commas or the like, they're raw, pure numbers
As such, something like
tonumber(UserText)
Doesn't quite always do the trick.
That's where string patterns come in handy. Something like so should do:
--... UserText=UserText:gsub("%D","") --Replace based on a pattern! Anything not a digit *or* a preceding - is removed. --...
That's of course assuming you're not allowing negative values or the like, but this is just an example.
Read more over at https://www.lua.org/pil/20.2.html