# I need help with merging diapasons

I’m making game like Bejeweled. Almost everything works fine, until you match gems in “air”.

How it ever working?
I have 2 diapasons: for example

1-3 and 4-6
They should merge into 1-6.

1-2 and 5-7
They shouldn’t merge

1-7 and 3-5
They should merge into 1-7

Currently, my script occasionly sometimes not merge diapasons, and make them like:

Was: 1-3, 4-6
“Merged”:1-3, 3-6,
But sometimes, the exact same combination gives correct result:

Was: 1-3, 4-6
Merged:1-6,

Can someone help me and say - what I’m making wrong?

``````for a = 1, #Destroyed[LenghtY[i][1]], 1 do
if (Destroyed[LenghtY[i][1]][a][1] >= Min) and (Destroyed[LenghtY[i][1]][a][1] <= Max + 1) then
Destroyed[LenghtY[i][1]][a][1] = Min
Inserted = true
end
if (Destroyed[LenghtY[i][1]][a][2] <= Max) and (Destroyed[LenghtY[i][1]][a][2] >= Min - 1) then
Destroyed[LenghtY[i][1]][a][2] = Max
Inserted = true
end
end
if Inserted == false then
table.insert(Destroyed[LenghtY[i][1]], {Min, Max})
end
``````

Destroyed[LenghtY[i][1]][a] - Diapason, which tested for merge, [1] - first number and [2] - second
Min and Max - Numbers of Diapason which I want insert or merge.

(I know, this’s really hard to understand, I’ll add images tomorrow, bc I can’t rn.)