When do loops start to lag?

I had a loop while task.wait(0.1) with 5 for _, loops and other checks and it really lagged the game.

‘The Loop basically gathers all of the player data and checks if an action can be done’

The loop is because the data can update, but a downside is the lag ,which is pretty obvious. But is there a way to optimize loops without making an event for everything changing (its hard to explain here)

while task.wait(0.1) do
--for example
for _, in pairs(data) do
for _, folder in pairs(storage)  do
if data then

end
end
end
end

The trick is to only do checks when you know its possible the thing you’re checking for could have changed. So for example if you’re checking how many players there are, you only need to do that when a player is added or removed from Players.