Jump to content




checking what type of a variable something is


  • You cannot reply to this topic
3 replies to this topic

#1 Exerro

  • Members
  • 801 posts

Posted 04 January 2013 - 10:18 PM

is there a way to check if a variable is a number or and boolean or something without the code crashing if it isnt?

I have tried tonumber for numbers but that would work with "3" etc and i also tried if n/4 then but that crashed the code :l

Please help :P

#2 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 04 January 2013 - 10:41 PM

use the type() function

local a = 1
print(type(a)) -- will print number


#3 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 04 January 2013 - 10:42 PM

type()

t = { var= 3, state= true, message = "Hello, I am a string" }

for i, v in pairs(t) do
	print(i .. " is " .. v .. " which is a " .. type(v))
end


#4 Exerro

  • Members
  • 801 posts

Posted 04 January 2013 - 10:48 PM

wow i cant believe it is that simple *facepalm" thanks :D





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users