Fix integer value going passed a certain number

I’m having a minor problem with my code. I’m most likely overthinking it and may end up solving the problem myself. Since my project is a personal project, and I’d rather not leak my code - I’ll put it in a simplified way.

For my issue, I am using an integer variable for logging purposes. When the user clicks a button, the variable is supposed to go up by 1, but needs to reset to 0 after it hits 6. However, the variable doesn’t reset until it hits 7. Below is a simplified form of my code. There’s a lot of stuff here, and formatting may not be the greatest, so please try and look at it the best you can.

local button = script.Parent -- this is the button a user clicks.
local x = 0 -- the integer variable we're talking about here
local a = false -- assume this is true later in my example
local function changeLog()
      if a == true then
            x = x + 1
            print(x)
     end
     if x == 1 then -- real code does more, but integer variable doesn't change. so, we'll just output the variable
            print(x)
    else if x == 2 then
                print(x)
       else if x == 3 then
                   print(x)
           else if x == 4 then
                      print(x)
              else if x == 5 then
                         print(x)
                   else if x == 6 then
                              print(x)
                       else if x >= 6 then
                                   x = 0
                                   print(x)
                           end
                        end
                   end
                end
            end
        end
    end
 end

button.Activated:connect(changeLog)

Like I said, I may be overthinking it. It’s probably just a simple solution, where I say “else if x == 6 then do this” or something like that. Thanks for taking the time to read my post. Anything helps. :slight_smile:

Try replacing all “else if” with “elseif”.

Also, why making elseifs statement for printing if you can just print it?

I have a lot more going on inside the elseifs than just printing. It doesn’t change the integer though. It applies changes to a GUI. Do you know what Linux is? I’m making a CLI similar to that where the user can see a history of commands they’ve entered.

Hello! It seems you’re saying else-if instead of elseif. That may the problem. In addition to that i’d recommend avoiding long if statements by using a Dictionary containing functions for each key or number action in your case.

1 Like

Alright. Thanks for the suggestion. I’ll try and come up with a way to shorten down the statements. I’m a beginner at Lua, so I’ll have to figure out a way to do this.

Here is an example of something you could possibly do.

I’m new to the dev forum so sorry if i created a new thread or something like that.

local num = 0

local actionDict = {
	[1] = function()
		print("Hey!")
	end,
	[2] = function()
		print("Hey!")
	end,
	[3] = function()
		print("Hey!")
	end,
	[4] = function()
		print("Hey!")
	end,
	[5] = function()
		print("Hey!")
	end,
	[6] = function()	
		print("Hey!")
		num = 0
	end,
}
button.MouseButton1Click:Connect(function()
	actionDict[num]
end)
1 Like

Try printing the a variable every time changeLog() runs, maybe it’s always false?

1 Like

Thanks for the example. Shortening it down actually helped. In addition, I’m no longer having that issue. Thanks for your help! :slight_smile:

1 Like

Glad to here! Good luck on your project!

1 Like