Understanding the typeof() function in Roblox

What is the typeof() function in Roblox and what is it useful for?

To kick off, typeof() is a very special function made by Roblox which returns a specified datatype as a string.

print(typeof(1)) -- Prints out datatype number
print(typeof("")) -- Prints out datatype string
print(typeof(false)) -- Prints out datatype boolean

Already this is useful for:

  • Preventing exploiters from sending different datatypes to break your scripts or send multiple error messages in the output
  • Helping scripts check a returned datatype and then run certain code afterwards
  • Adding an extra layer of security to your game

Examples that use the typeof() function

I will now show you some useful examples that use the typeof() function and explain how it each example works along the way.

This useful example uses the typeof() function to check what datatype a variable is holding.

local MyValue = 10

if typeof(MyValue) ~= "number" then -- Checks if the variable is a holding a number or not
	print("Invalid datatype!")
else
	print("Valid datatype!")
end

This useful example uses the typeof() function to check what datatype is being returned from the client.

RemoteEvent:FireServer("Hello World!") -- Client sends this string to the server
RemoteEvent.OnServerEvent:Connect(function(Player, Value) -- The server receives the RemoteEvent sent by the client and the value sent across as well
	if typeof(Value) ~= "string" then -- Checks if the value sent by the client is a string or not
		print("Invalid datatype!")
	else
		print("Valid datatype!")
	end
end)

Why do we use the typeof() function instead of the type() function?

You may have already noticed that there is also another function called type() which sort of works the same as the typeof() function. Except, the only difference is the typeof() function is a Roblox Global whereas the type() function is an Lua Global.

In conclusion, it is recommended that developers use the typeof() function on Roblox instead of the type() function.

If you would like to read up more on the typeof() function and the type() function, here are the documentation pages: Roblox Globals | Roblox Creator Documentation, Lua Globals | Roblox Creator Documentation

I hope this helps.

24 Likes

Found all of this information in 2 less than minutes that more or less explains what’s being explained here, was a community tutorial really necessary…?

5 Likes

I still use type() if possible.

image

Code
local s = os.clock()
for i = 1, 10000000 do
	type(i)
end
print('type():',os.clock()-s)

task.wait(2)

s = os.clock()
for i = 1, 10000000 do
	typeof(i)
end
print('typeof():',os.clock()-s)

That is definitely micro-optimization and should be completely ignored. Take each number and divide it by 10 million to get the true difference each iteration (did my own check):
type(): 0.04487640003208071
typeof(): 0.060179200023412704
0.0000000060179200023412704 - 0.000000004487640003208071
time difference each iteration: 0.0000000015302799991331994

This means type() is 0.00000000153 seconds faster.

Most of the comments with “Use this instead of that” actually disprove themselves within their own benchmarks. To put it into perspective, it’s like putting a bottle of water inside a shipping container and saying: “If I put 10 million bottles of water in the shipping container, then the shipping container will be overflowing, so don’t put that single bottle of water inside the shipping container.”

22 Likes

type() should be used for vanilla Lua types. typeof() for Roblox types.

4 Likes

This is what I do.

If it’s a number, boolean, or string, I use type(), but if it’s an instance, I use typeof().

For people other than VortexColor reading this, most Roblox-specific variable types return “userdata” if checked with type(), which is useless. One catch with typeof() is that the returned strings are capitalized, while basic Lua types aren’t! For example, strings return “string”, but instances return “Instance”.

If I’m unsure of what typeof() will return, I use the command bar to make a variable then print its type.

2 Likes

I’m so tired of people trying to invalidate community tutorials because they could google something. I didn’t really know the uses or how it works 100% until I read this.

15 Likes

That is not a micro optimization, that is a huge optimization which can cause a huge delay. There’s really no reason to use typeof unless you’re working with lots of data types.

What would a table’s datatype be? “object” ?

it’s still just “table”

1 Like