Loop problem - "Script timeout: exhausted allowed execution time"

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
2 Likes

Someone has had a similar problem to you. Run this command in the command line inside of studio.

settings().Studio.ScriptTimeoutLength = -1
1 Like

I’m not sure if this is a good idea. Maybe he could just make a limit for parts or make the deleting run in few scripts/parts.

3 Likes

maybe try adding a wait() inside the loop…idk

1 Like

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.

This wouldn’t change anything.

yes i think it too, because i tried out it and it crashed.

yes i already tried it, but it is crashed too and after 20 000 items it starts making big lags.

1 Like

This error happens due to the amount of instances that you need to delete. Is it possible to limit the amount a client can place?

The script needs some type of waiting period.

Hi why you dont try adding a wait(0.1) this worked for me

yes but there is a problem if other people in your team have big building too, it will make unstoppable loop.

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
1 Like

Performance wise could you try to use debris service?

yes but is there a way to do it without limit? Like cut table into more smaller parts.

Make parts just a texture or something. The best solution.

Either way, 20k parts for only one clients build is just insane, possibly use meshes.

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.

1 Like

yes but i think that there is a way. Because I can build very big park in Water Park world or experiences like it. I dont know how they are doing it.

There is definitely a way. We just don’t know how these guys from Water Park do it. Maybe try to ask them how they do it?

1 Like

They use simple parts/meshs. Not complex buildings.

1 Like