@Sarah Wesker i applied your sugestion but ended up with this
local function effectLoop(position, effect)
local bagIds = {1991, 1992, 1993, 1997, 1996}
local active = false
for _, bag in ipairs(bagIds) do
local lootBag = Tile(position):getItemById(bag)
if...