I am trying to loop through a dictionary of player stats, but am having trouble finding a way to make sure a value is a table vs a number value.
My Code:
01 | Players [ player ] [ "Stats" ] = { |
02 | Chest = { Pectorals = 1 } , |
03 | Arms = { Biceps = 1 , Triceps = 1 } , |
04 | Legs = { Quads = 1 , Glutes = 1 , Hamstring = 1 , Adductors = 1 } , |
05 | Core = { Abs = 1 , LowerBack = 1 } , |
06 |
07 | } |
08 | Players [ player ] [ "Stats" ] [ "Stats" ] = Instance.new( "Folder" ,player) |
09 |
10 | StatChangeEvent.OnServerEvent:Connect( function (plr) |
11 | local output = 0 |
12 | local function loop() |
13 | for i,v in pairs (Players [ plr ] [ "Stats" ] ) do |
14 | if v [ 1 ] then |
15 | loop(v) |
You can use the tonumber
function to see if something is a number or not. If something can be converted into a number, it returns that number, otherwise, it returns nil.
1 | local t 1 = { Jeff = "gee" ,gdhg = "ooga nooba" } |
2 | local t 2 = { "hey" , 122 } |
3 |
4 | print ( tonumber (t 1 )) --nil |
5 | print ( tonumber (t 2 )) --nil |
6 | print ( tonumber ( 1 )) --1 |
7 | print ( tonumber ( "1" )) --1 |
you could also use the type function to see if the type of an object is a table/number
1 | local t 1 = { Jeff = "gee" ,gdhg = "ooga nooba" } |
2 | local t 2 = { "hey" , 122 } |
3 |
4 | print ( type (t 1 )) --table |
5 | print ( type ( 25532 )) --number |
Addition:
If you want to see if something like a string contains a number like:
"happy 57th birthday!"
you would have to use the match function of the strings library to get it
1 | local str = "happy 57th birthday" |
2 |
3 | print (str:match( "%d+" )) --57 |
(%d+ gets 1 or more repetitions of digits)
I'm assuming you mean this?
01 | Players [ player ] [ "Stats" ] = { |
02 | Chest = { Pectorals = 1 } , |
03 | Arms = { Biceps = 1 , Triceps = 1 } , |
04 | Legs = { Quads = 1 , Glutes = 1 , Hamstring = 1 , Adductors = 1 } , |
05 | Core = { Abs = 1 , LowerBack = 1 } , |
06 |
07 | } |
08 | Players [ player ] [ "Stats" ] [ "Stats" ] = Instance.new( "Folder" ,player) |
09 |
10 | StatChangeEvent.OnServerEvent:Connect( function (plr) |
11 | local output = 0 |
12 | local function loop() |
13 | for i,v in pairs (Players [ plr ] [ "Stats" ] ) do |
14 | if v [ 1 ] then |
15 | loop(v) |