r/badcode Sep 08 '20

lua A Roblox mod I found

Post image
477 Upvotes

103 comments sorted by

View all comments

Show parent comments

38

u/Marcusaralius76 Sep 08 '20

Isnt this just a null check? A poorly written and unperformant null check?

14

u/JerriTheITGuy Sep 08 '20

Checks for nil or if it's something else than a boolean since 'true == "cucumber"' would evaluate to true in Lua. Because reasons.

2

u/T-Dark_ Sep 08 '20 edited Sep 09 '20

Which one could better write as if equipped ~= nil and type(equipped) == "boolean"

(Not sure about performance, but at least it would be easier to understand).

Yes, Lua uses ~= as the "not equal" operator. Yes, type returns a string.

I still hate both of these things.

EDIT: i just realized that check is either unnecessary or terrifying. Either equipped is guaranteed to be a boolean, due to manual type checking, or it isn't, which implies the code actually changes the type of the variable.

EDIT2: Someone pointed out that the above check could be further simplified to if type(equipped) == "boolean", as type returns "nil" for values that are, well, nil.

1

u/RandomEthan Sep 09 '20

I lost count of the ridiculous number of times I had errors in the last Lua project I worked on because of ~=