Trying to loop through a dictionary and destroy values which are too far from the player, but for some reason it sometimes randomly errors with
invalid key to ‘next’
Looked around and seems like this error is caused because I’m removing values from the dictionary at the same time I’m looping through it which causes it to get confused.
for position, chunk in pairs(Chunks) do local Distance = (chunk.PositionValue.Value - Vector3.new(Camera.CFrame.X,0,Camera.CFrame.Z)).magnitude if Distance > 700 then chunk:Destroy() Chunks[position] = nil wait() end end
Any way to go around this error?