attempt to compare number with nil?
Asked by
5 years ago Edited 5 years ago
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.
01 | function module:ConvertComma(num) |
02 | local x = tostring (num) |
04 | local important = (#x- 9 ) |
05 | return x:sub( 0 ,(important)).. "," ..x:sub(important+ 1 ,important+ 3 ).. "," ..x:sub(important+ 4 ,important+ 6 ).. "," ..x:sub(important+ 7 ) |
07 | local important = (#x- 6 ) |
08 | return x:sub( 0 ,(important)).. "," ..x:sub(important+ 1 ,important+ 3 ).. "," ..x:sub(important+ 4 ) |
10 | return x:sub( 0 ,(#x- 3 )).. "," ..x:sub((#x- 3 )+ 1 ) |
16 | function module:ConvertShort(Filter_Num) |
17 | local x = tostring (Filter_Num) |
19 | local important = (#x- 9 ) |
20 | return x:sub( 0 ,(important)).. "." ..(x:sub(#x- 7 ,(#x- 7 ))).. "B+" |
22 | local important = (#x- 6 ) |
23 | return x:sub( 0 ,(important)).. "." ..(x:sub(#x- 5 ,(#x- 5 ))).. "M+" |
25 | return x:sub( 0 ,(#x- 3 )).. "." ..(x:sub(#x- 2 ,(#x- 2 ))).. "K+" |
and here is an example of the purchasing script: (localscript)
01 | local Settings = require(game.workspace.Settings) >>I added this after to see if it would help, but no. |
03 | local plr = game.Players.LocalPlayer |
04 | local backpack = game.Players.LocalPlayer.Backpack |
05 | local tool = game.ReplicatedStorage.Cinnabun |
09 | script.Parent.MouseButton 1 Click:Connect( function () |
11 | if tonumber (plr.leaderstats.Cash.Value) > = 5 then |
13 | if not plr:FindFirstChild( "Cinnabun" , true ) and not plr.Character:FindFirstChild "Cinnabun" then |
15 | plr.leaderstats.Cash.Value = tonumber (plr.leaderstats.Cash.Value) - 5 |
17 | tool:Clone().Parent = plr.Backpack |
If there are any suggestions you may have or if you have run into this issue any thought would be much appreciated!
Cheers!