SwitchCase Module for replacing big if statements!

I’ve been working on a module that can help clean up the way longer if statements look.

It can check and compare any type of value given to it (strings, numbers, booleans, Vector3s, parts, etc.) and run any function attached.

Here's what the module itself looks like

You can place it wherever you like, or even clone it, but I like to keep it in ReplicatedFirst because it’s a nice universal spot where any script can require it.

I’ve decided I want to make a fan change between power levels when you click on the corresponding buttons using my switch statement.

Here's what it looks like.

Fan

It has 6 buttons: four power levels, and two buttons for power on/off. 3 of these buttons have nothing to do, so they’ll share functions with other buttons, provided they come next in the list.

How to set up a case function

How to use the Switch Statement
local switch, case = unpack(require(ReplicatedFirst.SwitchCase))

3 buttons don’t have functions assigned, but their names do appear in the case list, so the switch statement will find them, and then iterate down until it reaches the next viable function.

“On” and “Medium” both have nothing in them, but they do come before another case that does. The switch statement will run the first case it finds after both of the blanks.

Let’s watch it work:

Here’s a link to the module, with the fan included!

Let me know what you think! If you can think of any way to make the module more efficient, I’d really appreciate any advice!

2 Likes

That seems like an extraordinarily lot of work compared to other Luau switchcase modules or other programming languages’ native switchcase function (VB .NET, C#, C++, etc). It’s also interesting that your module does not incorporate breaks, but rather relies on the case function to return end. Can you explain why this module would be better than others? Since it seems like an effort to create the case functions.

1 Like

Why use this over look up functions?