Type() in Lua return nil for false

So I’m doing some type checking in Lua. And I noticed that the type() function returns nil for booleans.

Now correct me if I’m wrong. But true and false are boolean values.
False is equal to nil. And anything that’s not false or nil, is true.

but type(false) returns “nil”.

It’s really weird. I’m quite literally using the false.

Here’s some of the code I’m using.

local DataType = {}
DataType["number"] = "NumberValue"
DataType["string"] = "StringValue"
DataType["boolean"] = "BoolValue"
DataType["nil"] = "BoolValue" -- Had to add this because type(false) returns nil.

And this is how the data looks, there’s a function with a for loop that creates containers for a given type that correlate to a given value instance as seen above.

local Container = Instance.new(DataType[type(v)])
Container.Name = k
Container.Value = v

If I was to send this table to this function for instance.

Moderation = {Banned = false, Limited = false, WarnHistory = {}, BanHistory = {}, ForceWipe = false}

Banned, Limited, and ForceWipe all return their type as nil. Is this intended behavior? And is there some work around?

I do not get this behaviour. What do you think is causing the result to be nil?
image

2 Likes

If Container.Value is a integer, this will not work, because unlike arrays, you cannot reference dictionary entries as numbers.

You’ll need to manually reference each entry.

Moderation = {Banned = false, Limited = false, WarnHistory = {}, BanHistory = {}, ForceWipe = false}

DataType[type(Moderation.Banned)]
1 Like

Not convinced you’ve debugged this quite accurately - it looks like it’s failing when it hits a value of type “table”.

I had a go at reproducing your test code. I’ve commented out the table-valued entries.

local DataType = {}
DataType["number"] = "NumberValue"
DataType["string"] = "StringValue"
DataType["boolean"] = "BoolValue"
DataType["nil"] = "BoolValue"

local function MakeContainer(k, v)
	print(("Key: %s, type(Value): %s"):format(k, type(v))) -- added
	local Container = Instance.new(DataType[type(v)])
	Container.Name = k
	Container.Value = v
end

local Moderation = {
	Banned = false, 
	Limited = false, 
	-- WarnHistory = {},
	-- BanHistory = {},
	ForceWipe = false,
}

for k, v in next, Moderation do
	MakeContainer(k, v)
end
--> Key: Banned, type(Value): boolean
--> Key: Limited, type(Value): boolean
--> Key: ForceWipe, type(Value): boolean
2 Likes

@AbiZinho

The Container.Value differs based on the type that’s returned. So it’s not always an integer, its more like it creates a container in which the value can be accepted. I’m iterating through this, and my implementation looks more like @sircfenner 's

sircfenner is correct though, I didn’t debug this correctly. My table which shouldn’t actually have any nil values, was and still is returning nil for some odd value that occasionally appears (Time for the hunt).

It is indeed returning as a boolean, It was an oversight on my part, I did not print k,type(v) as I should have. Instead I printed type(v) and assumed otherwise. So now it’s all about looking for that nil value.

Thanks :blush: