Hi,
I have a problem with a loop, i am using for loop to delete all player’s buildings on his plot. When player buildings count is almost 20 000, the loop crashes with error: “Script timeout: exhausted allowed execution time”. I tried to make loop multi tread but it is not helping so much or i tried to add wait on every 1000 items but it is not helping so much too. I have no idea how to do it.
Thanks for any help
My code:
for _,item in pairs(plot.Models:GetChildren()) do
if item:FindFirstChild("Data") and item:FindFirstChild("Data"):FindFirstChild("Owner") and (item:FindFirstChild("Data"):FindFirstChild("Owner").BuiltBy.Value == plr or item:FindFirstChild("Data"):FindFirstChild("Owner").Value == plr) and item:FindFirstChild("Data"):FindFirstChild("CanBeDeleted") and item:FindFirstChild("Data"):FindFirstChild("CanBeDeleted").Value then
item:Destroy()
end
end
Might be a bit tedious, but how about instead of adding wait on every 1000 items you instead break out of the loop and then start the loop again until there are no more items left.
I think you can make the condition check slightly more efficient like this (using Lua short circuiting):
Don’t know if this gonna fix your problem, but it’s a good practice:
if item:FindFirstChild("Data") and item.Data:FindFirstChild("Owner") and (item.Data.Owner.BuiltBy.Value == plr or item.Data.Owner.Value == plr) and item.Data:FindFirstChild("CanBeDeleted") and item.Data.CanBeDeleted.Value then
yes but it is working when you are deleting small count of items, but if you are deleting 40 000 items it is not working it will crash or make very big lags.