The global space is shared by the entire Blizzard UI and all the 3rd party addons you have installed. Naming a global variable "i" is not a great idea as it's too easy for it to be "trampled" by someone missing a "local" declaration by accident.
Code:
local Mining, Herbing = 2580, 2383
local FindHerbs
local function tracking()
if not UnitIsEnemy("player","target") and not IsResting() then
FindHerbs = not FindHerbs
if FindHerbs then
CastSpellByID(Herbing)
else
CastSpellByID(Mining)
end
end
end