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

Finding if a value is a table or a number?

Asked by 5 years ago

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:

     Players[player]["Stats"] = {
        Chest = {Pectorals = 1},
        Arms = {Biceps = 1, Triceps = 1},
        Legs = {Quads = 1, Glutes = 1, Hamstring = 1, Adductors = 1},
        Core = {Abs = 1, LowerBack = 1},

    }
    Players[player]["Stats"]["Stats"] = Instance.new("Folder",player)

    StatChangeEvent.OnServerEvent:Connect(function(plr)
        local output = 0
        local function loop() 
            for i,v in pairs(Players[plr]["Stats"]) do
                if v[1] then
                    loop(v)
                    print(v[1])
                elseif v then
                    output = output + v
                    print(v)
                else
                    print("Failure")
                end
            end
            end
            loop()
        local PowerLevel = output
        plr.leaderstats["Power Level"].Value = PowerLevel
    end)
end)
0
You can use type(variable) == "number" to check if it is a number or not. Using tonumber(variable) could cause false positives where a string could be considered a number value, which could be exploited and cause bugs. Overscores 381 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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.

local t1 =  {Jeff = "gee",gdhg = "ooga nooba"}
local t2 = {"hey",122}

print(tonumber(t1))--nil
print(tonumber(t2))--nil
print(tonumber(1))--1
print(tonumber("1"))--1

you could also use the type function to see if the type of an object is a table/number

local t1 =  {Jeff = "gee",gdhg = "ooga nooba"}
local t2 = {"hey",122}

print(type(t1))--table
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

local str = "happy 57th birthday"

print(str:match("%d+"))--57

(%d+ gets 1 or more repetitions of digits)

0
Good stuff DinozCreates 1070 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

I'm assuming you mean this?




Players[player]["Stats"] = { Chest = {Pectorals = 1}, Arms = {Biceps = 1, Triceps = 1}, Legs = {Quads = 1, Glutes = 1, Hamstring = 1, Adductors = 1}, Core = {Abs = 1, LowerBack = 1}, } Players[player]["Stats"]["Stats"] = Instance.new("Folder",player) StatChangeEvent.OnServerEvent:Connect(function(plr) local output = 0 local function loop() for i,v in pairs(Players[plr]["Stats"]) do if v[1] then loop(v) print(v[1]) elseif type(v) == 'table' then -- I'm assuming you mean this? for VaraiableName,Value in pairs(v) do print(VaraiableName) output = output + Value end print(output) else print("Failure") end end end loop() local PowerLevel = output plr.leaderstats["Power Level"].Value = PowerLevel end) end)
0
that doesn't work, you have to use tonumber() DeceptiveCaster 3761 — 5y

Answer this question