How would I optimize this?

This is a script that welds blocks to its neighbors and it is very costly, it gets to a peak of 40 in activity and I need help optimizing and its used frequently

game.ReplicatedStorage.Remotes.Launch.OnServerEvent:Connect(function(Plr,bool)
	local BuildModel = workspace.Builds:FindFirstChild(Plr.Name)
	local Char = Plr.Character
	if bool == true and BuildModel then
		
		local Blocks = BuildModel:GetChildren()
		for i = 1,#Blocks do
			local Primary = Blocks[i].PrimaryPart
			--[[local Weld = Instance.new("WeldConstraint",Primary)
			Weld.Part0 = BuildModel.PrimaryPart
			Weld.Part1 = Primary]]
			
			local PartsInRegion
			if Primary.Size == Vector3.new(4,4,4) then
				local rg1 = Primary.Position + Primary.Size/1.9
				local rg2 = Primary.Position - Primary.Size/1.9
				local region3 = Region3.new(Vector3.new(math.min(rg1.X, rg2.X), math.min(rg1.Y, rg2.Y), math.min(rg1.Z, rg2.Z)),Vector3.new(math.max(rg1.X, rg2.X), math.max(rg1.Y, rg2.Y), math.max(rg1.Z, rg2.Z)))
				PartsInRegion = workspace:FindPartsInRegion3(region3,Primary)
			else
				local region3 = RotatedRegion3.new(Primary.CFrame, Primary.Size*1.1) -- cframe of the part and it's size
				PartsInRegion = region3:FindPartsInRegion3(Primary.Parent, math.huge) -- the ignore model, and the max parts that it can chose
			end
			
			for r = 1,#PartsInRegion do
				local Close = PartsInRegion[r]
				if Close then
					local check = false
					local ConnectedParts = Close:GetConnectedParts()
					
					for z = 1,#ConnectedParts do local CPart = ConnectedParts[z]	if CPart == Primary then check = true	break	end end --CheckIfAlreadyWelded
					
					if Close.Parent:FindFirstChild("ConnectWeld") then
						if Close == Close.Parent:FindFirstChild("ConnectWeld").Value and check == false then
							local Weld = Instance.new("WeldConstraint",Primary)
							Weld.Part0 = Close
							Weld.Part1 = Primary
						end
					else
						if Close.Name == "HitBox" and check == false then
							local Weld = Instance.new("WeldConstraint",Primary)
							Weld.Part0 = Close
							Weld.Part1 = Primary
						end
					end
				end
			end
		end
		
		--local Clone = BuildModel:Clone()
		--Clone.Parent = StoredBuildClones
		
		local Stuff = BuildModel:Clone():GetChildren()
		
		
		for i = 1,#Blocks do
			local Primary = Blocks[i].PrimaryPart
			Primary.Anchored = false
		end
		
		local Ran = 450
		
		if BuildModel and BuildModel.PrimaryPart then
			BuildModel:SetPrimaryPartCFrame(CFrame.new(Vector3.new(math.random(-Ran,Ran),10,math.random(-Ran,Ran))))
		else
			if WarnDebug == true then
				warn(Plr.Name.." Build had no PrimaryPart. finding primarypart.....")
			end
			if BuildModel and BuildModel:FindFirstChild("Chassis") and BuildModel:FindFirstChild("Chassis").PrimaryPart then
				BuildModel.PrimaryPart = BuildModel:FindFirstChild("Chassis").PrimaryPart
				
				if WarnDebug == true then
					warn(Plr.Name..". PrimaryPart Found and Set")
				end
				
				if BuildModel and BuildModel.PrimaryPart then
					BuildModel:SetPrimaryPartCFrame(CFrame.new(Vector3.new(math.random(-Ran,Ran),10,math.random(-Ran,Ran))))
					elseif WarnDebug == true then
					warn(Plr.Name..". PrimaryPart Found and Set. But Not Found?")
				end
				
			end
		end
		
		local NewModel = Instance.new("Model")
		NewModel.Parent = StoredBuildClones
		NewModel.Name = BuildModel.Name
		
		for i,v in pairs(Stuff) do
			if v then
				v.Parent = NewModel
				RunService.Heartbeat:Wait()
			end
		end
		
	elseif bool == false and BuildModel then
		game.Debris:AddItem(BuildModel,0)
		
		if StoredBuildClones:FindFirstChild(Plr.Name) then
			StoredBuildClones:FindFirstChild(Plr.Name).Parent = workspace.Builds
		elseif not game.Workspace.Builds:FindFirstChild(Plr.Name) then
			if WarnDebug == true then
				warn("BuildModel Not Found, created new build model. "..Plr.Name)
			end
			
			local BuildModel = Instance.new("Model",workspace.Builds)
			BuildModel.Name = Plr.Name

			local Chassis = game.Lighting.OtherBlocks:WaitForChild("Chassis"):Clone()
			Chassis:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0, 10000,Plr:WaitForChild("Index").Value*200) - Vector3.new(0,36,0)))
			Chassis.Parent = BuildModel

			BuildModel.PrimaryPart = Chassis:WaitForChild("HitBox")
		end
		
		if Char then
			repeat wait() until Char and Char.PrimaryPart do
				Char:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0, 10000,Plr:WaitForChild("Index").Value*200)))
			end
		end
	end
end)
1 Like

I have fixed this by changing region3’s to
GetTouchingParts and [ Simple trick to make GetTouchingParts work with non-CanCollide parts](Simple trick to make GetTouchingParts work with non-CanCollide parts)

1 Like