How can I check the types of certain variables?

Hi, I have two things

  1. If i have a variable that is assigned to a player instance, how can i check what type is that variable and apply a check so if variable…type. == “PlayerInstance” then… print(“it’s a player!”) end?

  2. and also is there way to check what class type a variable is, for example a folder?

Use the instance’s .ClassName property to find what it is if you don’t know what it is, however you can use the instance’s :IsA method to find whether something is an instance or inherits an abstract class (BasePart, GuiBase, GuiObject, ValueBase etc).

3 Likes

I believe :IsA() is a better approach.

They both do similar things, but they also have their differences. :IsA() respects inheritance, while ClassName does not.

1 Like

use the typeof function, i.e. print(typeof(variable))

1 Like

and for the folder question, use instance:IsA(‘Folder’)

  1. If i have a variable that is assigned to a player instance, how can i check what type is that variable and apply a check so if variable…type. == “PlayerInstance” then… print(“it’s a player!”) end?

You can use the type function:

local Test = game.Players.LocalPlayer
print(type(Test)) -- userdata

Example in a if statement:

local Test = game.Players.LocalPlayer
if type(Test) == 'userdata' then
	print(Test)
end

More information on type and typeof:


  1. and also is there way to check what class type a variable is, for example a folder?
    Check the answer above.

Keep in mind that is just an example.
The main difference with type is that it is also used in vanilla lua, typeof is a Roblox function.

1 Like

You can use the built-in functions typeof() and type() to check the types of values. To use them simply pass the literal value itself as an argument to either function or pass the variable of which is being used to store the value, for example:

type("Hello") --would result in the type string
type(1) --would result in the type integer/number
local function hi()
return
type(hi) --would result in the the type function