Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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.

01function module:ConvertComma(num)
02    local x = tostring(num)
03    if #x>=10 then
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)
06    elseif #x>= 7 then
07        local important = (#x-6)
08        return x:sub(0,(important))..","..x:sub(important+1,important+3)..","..x:sub(important+4)
09    elseif #x>=4 then
10        return x:sub(0,(#x-3))..","..x:sub((#x-3)+1)
11    else
12        return num
13    end
14end
15 
View all 29 lines...

and here is an example of the purchasing script: (localscript)

01local Settings = require(game.workspace.Settings) >>I added this after to see if it would help, but no.
02 
03local plr = game.Players.LocalPlayer
04local backpack = game.Players.LocalPlayer.Backpack
05local tool = game.ReplicatedStorage.Cinnabun
06 
07 
08 
09script.Parent.MouseButton1Click:Connect(function()
10 
11        if tonumber(plr.leaderstats.Cash.Value) >= 5 then
12 
13            if not plr:FindFirstChild("Cinnabun", true) and not plr.Character:FindFirstChild "Cinnabun" then
14 
15                plr.leaderstats.Cash.Value = tonumber(plr.leaderstats.Cash.Value) - 5
View all 24 lines...

If there are any suggestions you may have or if you have run into this issue any thought would be much appreciated!

Cheers!

1 answer

Log in to vote
0
Answered by 5 years ago

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

1tonumber(UserText)

Doesn't quite always do the trick.

That's where string patterns come in handy. Something like so should do:

1--...
2UserText=UserText:gsub("%D","") --Replace based on a pattern! Anything not a digit *or* a preceding - is removed.
3--...

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

0
This is my first post, I cleaned up the formatting a bit now. Thanks for the suggestion, I will attempt to implement a string pattern! scagnetti619 24 — 5y
Ad

Answer this question