Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Finding if a value is a table or a number?

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)

1 comment

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.

2 answers

✓ Accepted answer

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)

1 comment

0

Good stuff

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)

1 comment

0

that doesn't work, you have to use tonumber()

This discussion is preserved as it was. New answers, comments, and votes are closed.