Variable not incrementing in on_message() and it's following function (SOLVED)

I made a variable called “turn” that I increment through my game. It works fine until I go to the second level (not a separate collection, i just change the information in some tables) and reset the variable to 1 (which is what it started as) and it refuses to increment. The weird part is that it increments properly BEFORE the message is called but when it gets to the message it’s at its default value.

relevant code:

function update(self, dt)
................
num = num + .01 --
		if num > 1.01 then --also while I'm here, this didn't work with just 1 but it worked with 1.01 Any reason why?
			.......
			print("Turn is", turn) --prints 1
			turn = turn + 1
			print("Turn after is", turn) --prints 2
			num = .01
			.............
			if inputs > 0 then
				msg.post("#", "Execute")
			else
			level = level + 1
			end
			
			if level == 2 then
				battle2()
			end
		end
end

function on_message(self, message_id, message, sender)
    if message_id == hash("Execute") then
    	for i, t in pairs(tables)do --i have a table of tables that this thing is iterating through
    		print("Turn in message is", turn) --prints 1 even though before the message was sent in update, it was 2.
                                              --before the variable turn was set back to 1 in battle2() it would show the correct result
			if t[turn] ~= nil then
				fight(t,i) --Sends table
			end
		end
		..................
    end
end

function fight(enemy, i)
	print("Turn in fight is", turn) --prints 1 even though it's supposed to be 2
.......................................
end

function battle2()
    .............. --bunch of table changing stuff
    turn = 1
    ............
end

I would put in the entire script but there’s a crapton of if and elseif statements in my update() that I’m not entirely proud of (again, I will refactor it but getting this project in a working condition for school comes first) but if you need me to post the rest I can.

How is the turn variable defined? From the looks of it it is either a global variable or a variable local to the script. I’m sure this is a scoping issue of some kind. Search for variable scope here on the forum. I’m on my phone this weekend otherwise I would have linked it for you. I can give you better support on Monday.

The variable was local but I tried changing it to global and it still doesn’t work.

One possibility is that you are changing the turn back to 1 before the message is actually received.
Another possibility is the scoping, a cheap solution would be to convert the turn variable into a table and instead of
turn = turn + 1 write turn.value = turn.value + 1. Replacing turn with turn.value.

I am so many types of mad right now. It was a logical error. battle2() is called in update and it’s supposed to only happen under the condition that the level changed. However, they check kept running and even though turn got incremented and sent a message, turn later gets set BACK to 1 in the battle2() function. I changed the code to

if inputs > 0 then
				msg.post("#", "Execute")
			else
				level = level + 1
				if level == 2 then
					battle2()
				end
			end
end

since input will only ever be 0 when all the attacks have finished being processed/handled (which is when it needs to go to a new level).