How do I stop this error?

Hey guys. I was recently doing some testing and I figured out another point where you could use the “or” keyword. (or whatever “or” is known as) However, I found out that what I was trying to do wasn’t working and instead returned an error. Any way to fix this?

local function PrintEnum(Type)
     print(Enum.EasingStyle[Type] or Enum.EasingStyle.Linear)
end

PrintEnum(nil) -- Still errors even with nothing in it.

I expected it to print “Enum.EasingStyle.Linear” since the type was nil, but it returned an error instead. I’m not sure how to work around this. Huge thanks if you can help me out.

1 Like

Could you maybe post the error too here? (Didnt see the error, im blind)

2 Likes
local function PrintEnum(Type)
	print(Enum.EasingStyle[Type or "Linear"])
end

PrintEnum(nil) -- Still errors even with nothing in it.

I think that you were passing an EnumItem into the print function, that would resort in a error.

local function PrintEnum(Type)
     Type = Type or Enum.EasingStyle.Linear
     print(Type.Name)
end

PrintEnum(nil) -- Prints "Linear"

the problem is it’ll still index Enum.EasingStyle to check if the style exists and Enum.EasingStyle[nil] would give you “Attempt to index Enum.EasingStyle with nil” error

you should do what @yo9sa did instead

local function Launch(Target)
  print("Launching", Target or "nobody", "to the moon.")
end

local Target = nil
Launch(Target)
--> Launching nobody to the moon.

Thank you all for the help! I’ll keep this in mind for the future. Thanks again! :slight_smile: :grinning:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.