Please format your code. See this link about how to format your code. Put three backticks, then on the next line copy and paste all of your code, then put three more backticks.
You want to follow the pattern from the first part of this program
Here is the first part:
local enemy = hero:findNearestEnemy()
-- With AND, the type is only checked if enemy exists.
if enemy and enemy.type == "munchkin" then
The idea is to do the same thing in the next part, but instead of enemies, munchkins and attacking it will be items, coins and moving.