Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is the format good or is there a better way to write it in this script?

Asked by 8 years ago

Is the format good or is there a better way?

01local number = 1
02 
03function onMouseButton1Click()
04    if number == 1 then
05    elseif number == 2 then
06    elseif number == 3 then
07        number = 0
08    end
09    number = number + 1
10end
11 
12script.Parent.MouseButton1Click:connect(onMouseButton1Click)

Thanks!

1 answer

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

Referring to your "elseif" format

In Lua, there is no switch statement like other programming languages, so the whole "elseif" thing is the best way to do it. While there are other ways like using tables to connect functions together, it might just make it more complicated:

01local number=1
02function switch(c)
03  local swtbl = {
04    casevar = c,
05    caseof = function (self, code)
06      local f
07      if (self.casevar) then
08        f = code[self.casevar] or code.default
09      else
10        f = code.missing or code.default
11      end
12      if f then
13        if type(f)=="function" then
14          return f(self.casevar,self)
15        else
View all 33 lines...
Ad

Answer this question