Attempt to index local 'self' (a nil value) driving me crazy

I’ll start off by admitting that I have no clue why this is happening, because I have not had any issue using “self” in the past.

I am using an OOP outline that I believe I found on develephant. I’ll try and keep this simple.

local function Enemy(name, image, class) enemy.class = class enemy.image = image enemy.name = name enemy.maxHealth = 100 enemy.health = 100 enemy.strength = 5 enemy.armor = 10 enemy.target = Hero1 function enemyFollow(self, event) transition.moveTo (self, x, y, time = 1000) end enemy.enterFrame = enemyFollow -- moves enemies allows function to recognize self attributes Runtime:addEventListener("enterFrame", enemy) -- moves enemies function enemy:takeDamage( forHp, newTarget ) forHp = forHp or 0 self.health = math.max( 0, (self.health - forHp)) end end return Enemy

trying to keep it simple, this is a few lines of code taken out and simplified. As you can see, I have used both the colon and the dot operator for different functions. Both of these work and have given me no issues. I am able to add into each of these functions and use “self.health” or any other one of the specified labels and no issues have occurred.

Now, I am trying to add in a new function and I cant seem to figure out why I am getting the error “Attempt to index local ‘self’ (a nil value)”

I have tried this function

 function enemy:setRandomTarget () local newTarget = math.random( 1, 4 ) if not activeParty[newTarget].dead then if newTarget == 1 then print("hero1 should be new target") self.target = Hero1 elseif newTarget == 2 then print("hero2 should be new target") self.target = Hero2 elseif newTarget == 3 then print("hero3 should be new target") self.target = Hero3 elseif newTarget == 4 then print("hero4 should be new target") self.target = Hero4 end elseif activeParty[newTarget].dead == true then self:setRandomTarget() end end

I call this function with self:setRandomTarget() just as I would call the above function enemy:takeDamage by using self:takeDamage… Which has been working for the damage function… But when I try it with the setRandomTarget Function, I get the self nil error.

I have also tried to change the random target function to remove the colon and place it within my function that is on a runtime event listener. Seeing as I used the Dot operator there, I figured it would be able to recognize self, when I change the the function to setRandomTarget(self), but again, I get the same error.

At this point im about ready to throw the computer out the window.

Anyone have some insight on this

the way self is used matters to how the listener is setup. I have not messed with listeners in a while so i don’t recall the specifics, but there are multiple ways to setup listeners so play around with how you are setting them up and passing in the parms.

I hope this points you in the right direction.

the way self is used matters to how the listener is setup. I have not messed with listeners in a while so i don’t recall the specifics, but there are multiple ways to setup listeners so play around with how you are setting them up and passing in the parms.

I hope this points you in the right direction.