This script gives Script timeout: exhausted allowed execution time

ive tried looking and testing but no luck. please help.

while task.wait() do
	for i, blockparent in pairs(game.Workspace.Baseplate.GeneratedItems:GetChildren()) do
		if blockparent:FindFirstChild("Touching") then
			function getSurface(position, object)
				local surfaces = {
					Back = object.CFrame * CFrame.new(0, 0, object.Size.Z);
					Front = object.CFrame * CFrame.new(0, 0, -object.Size.Z);
					Top = object.CFrame * CFrame.new(0, object.Size.Y, 0);
					Bottom = object.CFrame * CFrame.new(0, -object.Size.Y, 0);
					Right = object.CFrame * CFrame.new(object.Size.X, 0, 0);
					Left = object.CFrame * CFrame.new(-object.Size.X, 0, 0);
				}
				local surface = "Back"
				for side, cframe in pairs (surfaces) do
					surface = ((position - cframe.Position).magnitude > (position - surfaces[surface].Position).magnitude and surface or side)
				end
				return surface
			end
			local block = blockparent:FindFirstChild(blockparent.Name)
			local partsfound = {}

			local foundParts = game.Workspace:GetPartsInPart(blockparent.Touching)
			if foundParts then
				for _, hit in pairs(foundParts) do

					if not hit:IsDescendantOf(blockparent) and not hit.Parent:FindFirstChild("Humanoid") and hit.Name ~= "Touching" and hit.Name == hit.Parent.Name then

						if partsfound[hit] == nil then
							local a = getSurface(hit.Position, blockparent.Touching)
							partsfound[hit.Name] = a
						end
						block[getSurface(hit.Position, blockparent.Touching)].Transparency = 1
					end
				end

				for index, num in pairs(partsfound) do
					if not table.find(foundParts, index) then
						block:FindFirstChild(num).Transparency = 0
						partsfound[index] = nil
					end
				end

			else
				block.Front.Transparency = 0
				block.Back.Transparency = 0
				block.Left.Transparency = 0
				block.Right.Transparency = 0
				block.Top.Transparency = 0
				block.Bottom.Transparency = 0
			end

		end
	end
end
3 Likes

why is this inside a loop that is always doing an iteration loop?

2 Likes

i cant seem to know what you mean, could you elaborate?

1 Like

i swear im about to ask gpt hahahahha

1 Like

You’re running way too many loops for no apparent reason, whats yuor approach?

2 Likes

ill comment on what im thinking in the code

function getSurface(position, object) --gets what surface the part is touching
	local surfaces = {
		Back = object.CFrame * CFrame.new(0, 0, object.Size.Z);
		Front = object.CFrame * CFrame.new(0, 0, -object.Size.Z);
		Top = object.CFrame * CFrame.new(0, object.Size.Y, 0);
		Bottom = object.CFrame * CFrame.new(0, -object.Size.Y, 0);
		Right = object.CFrame * CFrame.new(object.Size.X, 0, 0);
		Left = object.CFrame * CFrame.new(-object.Size.X, 0, 0);
	}
	local surface = "Back"
	for side, cframe in pairs (surfaces) do
		surface = ((position - cframe.Position).magnitude > (position - surfaces[surface].Position).magnitude and surface or side)
	end
	return surface
end



while task.wait() do -- starts the loop
	for i, blockparent in pairs(game.Workspace.Baseplate.GeneratedItems:GetChildren()) do --loops through the "generateditems" which is basically just all the blocks that are generated
		if blockparent:FindFirstChild("Touching") then --if the block parent has the hitbox 
			local block = blockparent:FindFirstChild(blockparent.Name)
			local partsfound = {}

			local foundParts = game.Workspace:GetPartsInPart(blockparent.Touching) -- cause the part is non collideable and i cant change it cause that would break a lot
			if foundParts then
				for _, hit in pairs(foundParts) do --here it checks all the parts that were found

					if not hit:IsDescendantOf(blockparent) and not hit.Parent:FindFirstChild("Humanoid") and hit.Name ~= "Touching" and hit.Name == hit.Parent.Name then -- just a long check
						local a = getSurface(hit.Position, blockparent.Touching)
						partsfound[hit.Name] = a
						block[getSurface(hit.Position, blockparent.Touching)].Transparency = 1 -- makes a texture transparent depending on what side its on
					end
				end

				for index, num in pairs(partsfound) do -- checks if the part is still in the table
					if not table.find(foundParts, index) then
						block:FindFirstChild(num).Transparency = 0
						partsfound[index] = nil
					end
				end

			else
				block.Front.Transparency = 0
				block.Back.Transparency = 0
				block.Left.Transparency = 0
				block.Right.Transparency = 0
				block.Top.Transparency = 0
				block.Bottom.Transparency = 0
			end

		end
	end

end
2 Likes

now it doesnt even work. if i take it slowly then…

1 Like

I have a question , are you trying to detect if theses part touch the player character via GetPartsinPart ?

nah, im trying to detect other parts with cancollide off

while true do

--loops n stuff

task.wait()
end

trust